locked
.change event not firing for first time on text box change event jquery. RRS feed

  • Question

  • User-574941244 posted

    Hi,

    I have 2 textboxes and on both I have applied .change event.

    But after firing .change event on first textbox when I change the text in 2nd textbox, its .change event does not fire first time.

    But after again change text in this textbox, the .change event works.

    Below is the applied Jquery event on both textbox.

    $('#<%=TextBoxPhone.ClientID %>').change(function () {

    });

      $('#<%=TextBoxAltPhone.ClientID %>').change(function () {

    });

    I have applied some searching on these change event. If record found in 1st textbox search then a value is entered in the 2nd textbox.

    If no record found then it is set to blank as shown below.

    $("#<%=TextBoxAltPhone.ClientID%>").val('');

     

    Please suggest some solution.

    Regards

    Vikas

     

    Tuesday, October 27, 2015 9:04 AM

Answers

  • User61956409 posted

    Hi Vikas,

    Welcome to ASP.NET forum.

    Do you mean that the second textbox change event will not be fired if you change the value of “TextBoxAltPhone” in “TextBoxPhone” change event? If that is the case, please refer to the following code to trigger change event dynamically.

    $("#<%=TextBoxPhone.ClientID %>").change(function () {
        //get the value of TextBoxAltPhone
        var oldval = $("#<%=TextBoxAltPhone.ClientID %>").val();
    
        //some searching, get result
    
        var searchresult = "thesearchingresult";
    
        $("#<%=TextBoxAltPhone.ClientID %>").val(searchresult);
         
        if (searchresult == oldval) {
            $("#<%=TextBoxAltPhone.ClientID %>").trigger("change");
        }
    })
    
    $("#<%=TextBoxAltPhone.ClientID %>").change(function () {
        //alert("hello1");
        //your code
    })
    

    Best Regards,

    Fei Han

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, October 28, 2015 2:29 AM

All replies

  • User632428103 posted

    Hello,

    can you give us your code, because i've just to create a sample page , add two text box and add on these text box the change event and it's work ..

    every time, if i type something in txt one and goes to text two i receive my alert message and the same thing for the other controls ..

    Perhpas with your code we can see what's really happens ..

            <script src="../../../Scripts/jquery-2.0.3.js"></script>
        <script type="text/javascript">
    
            $(document).ready(function ()
            {
                $('#txtOne').change(function () {
    
                    alert('change 1');
                });
    
                $('#txtTwo').change(function () {
                    alert('change 2');
                });
            });
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
            Enter value one
            <asp:TextBox ID="txtOne" runat="server"></asp:TextBox>
            <br />
            Enter second value :
            <asp:TextBox ID="txtTwo" runat="server"></asp:TextBox>
        </form>

    thanks

    Tuesday, October 27, 2015 9:54 AM
  • User61956409 posted

    Hi Vikas,

    Welcome to ASP.NET forum.

    Do you mean that the second textbox change event will not be fired if you change the value of “TextBoxAltPhone” in “TextBoxPhone” change event? If that is the case, please refer to the following code to trigger change event dynamically.

    $("#<%=TextBoxPhone.ClientID %>").change(function () {
        //get the value of TextBoxAltPhone
        var oldval = $("#<%=TextBoxAltPhone.ClientID %>").val();
    
        //some searching, get result
    
        var searchresult = "thesearchingresult";
    
        $("#<%=TextBoxAltPhone.ClientID %>").val(searchresult);
         
        if (searchresult == oldval) {
            $("#<%=TextBoxAltPhone.ClientID %>").trigger("change");
        }
    })
    
    $("#<%=TextBoxAltPhone.ClientID %>").change(function () {
        //alert("hello1");
        //your code
    })
    

    Best Regards,

    Fei Han

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, October 28, 2015 2:29 AM