locked
my code on onchange/keyup not works. RRS feed

  • Question

  • User932259438 posted

      $("#txtGive").change(function () {
    
            $("#dgear").addClass("Drive");
        var spdvalue = $("#Give").val();
    
            var initDegree = -120;
            var lastDegree = 120;
            var rotateDeg;
            if (spdvalue <= 240) {
                rotateDeg = initDegree + Math.round(spdvalue / 2);
    
            }
            else if (spdvalue > 240 && spdvalue < 480) {
    
                rotateDeg = Math.round(spdvalue / 2) - lastDegree;
    
            }

    I want to use on change after paste/put/entered text on txtGive

    $("#dvrunat").click(function () {
    $("#txtGive").val("3");
    })

    My code must works immediatelly after change txtGive.val().

    Currently works only if change in textbox. I must to click on textbox.

    If used dvrunat for entered text to txtGive.val() must to use my code.

    Please help




     

    Tuesday, February 4, 2020 4:50 PM

All replies

  • User-474980206 posted

    For a text box the change event fires on the blur event if the contents changed. Look at the input event, which fires on every change of the value.

    Wednesday, February 5, 2020 2:35 AM
  • User665608656 posted

    Hi progy85,

    As @bruce said, you can achieve your needs by triggering input events.

    The input event allows you to trigger the event immediately when pasting or putting, without requiring you to click again.

    For the click event to change the value of the input, you can achieve it by calling the event triggered by the input again.

    For details, you can refer to the following code:

    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title></title>
        <script src="../Scripts/jquery-3.4.1.min.js"></script>
        <script type="text/javascript">
            $(function () {
                $("#dvrunat").click(function () {
                    $("#txtGive").val("3");
                    ChangeGive();
                })
                $('#txtGive').on('input', function (e) {
                    ChangeGive();
                }); 
            });
            function ChangeGive() {
                $("#dgear").addClass("Drive");
                var spdvalue = $("#txtGive").val();
                var initDegree = -120;
                var lastDegree = 120;
                var rotateDeg;
                if (spdvalue <= 240) {
                    rotateDeg = initDegree + Math.round(spdvalue / 2); 
                }
                else if (spdvalue > 240 && spdvalue < 480) {
                    rotateDeg = Math.round(spdvalue / 2) - lastDegree;
                }
                alert(rotateDeg);
            }
        </script>
        <style>
            .Drive {
                width: 300px;
                height: 200px;
                background-color: antiquewhite;
            }
        </style>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                <div id="dgear">
                    <input id="txtGive" type="text" />
                    <input id="dvrunat" type="button" value="change give value" />
                </div>
            </div>
        </form>
    </body>
    </html>
    

    Here is the result of this work demo:

    Best Regards,

    YongQing.

    Wednesday, February 5, 2020 8:17 AM