locked
onchange always fires RRS feed

  • Question

  • User1510859543 posted

    We have an aspx page that we want to trigger when the contents of a textbox changes.  When it changes we set a hidden checkbox to checked.  However, this js code (setChange) is firing when we press the Tab key and not changing the textbox contents.  We want it to fire only when the textbox (or other control) contents change.  I have tried it both with and without the doKey() function and it does the same thing.  Below is sample markup.

            document.onkeydown = doKey;
    
            function doKey(e) {
                if (event.keyCode === 13) {
                    event.keyCode = 9;
                    return event.keyCode;
                }
            }
    
    
    
    
    function setChange() {
        var ckchg = document.getElementById('ckChangesMade');
        ckchg.checked = true;
    }
    
    
    <asp:TextBox ID="txtFirstName" runat="server" Text='<%# Bind("FirstName") %>' 
                 CssClass="bggreen" MaxLength="20" TabIndex="1" Width="200" onchange="setChange();" />

    Thursday, August 1, 2019 2:39 PM

Answers

  • User475983607 posted

    The code always sets the check.  Is that correct?

    function setChange() {
        var ckchg = document.getElementById('ckChangesMade');
        ckchg.checked = true;
    }

    Anyway, I tested onchange and it work exactly as written, the onchange fires only when the text input changes.  The issue must be elsewhere in your code.

    https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_onchange2

    Please use your debug tools to find the bug.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, August 1, 2019 3:16 PM

All replies

  • User475983607 posted

    The code always sets the check.  Is that correct?

    function setChange() {
        var ckchg = document.getElementById('ckChangesMade');
        ckchg.checked = true;
    }

    Anyway, I tested onchange and it work exactly as written, the onchange fires only when the text input changes.  The issue must be elsewhere in your code.

    https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_onchange2

    Please use your debug tools to find the bug.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, August 1, 2019 3:16 PM
  • User1510859543 posted

    Yes it does but not sure where to look as there is no js or jquery code that does anything in onblur. I will try to debug.

    Thursday, August 1, 2019 3:36 PM
  • User-474980206 posted

    the onchange fires on the blur event if the content has been modified.

    Thursday, August 1, 2019 8:59 PM
  • User-719153870 posted

    Hi dlchase,

    The onchange event happens when both an object loses focus and text changed.

    The onblur hapeens when only if an object loses focus. Onblur

    We can't find an onblur event in your txtFirstName while you still got your problem which is strange.

    Maybe you can try add an onblur event to txtFirstName and do nothing? 

    For example:

    <script type="text/javascript">
            function setChange() {
                //var ckchg = document.getElementById('ckChangesMade');
                //ckchg.checked = true;
                alert('Changed');
            }
            function setBlur() {
                //alert('Blur');
            }
        </script>
    
     <asp:TextBox ID="TextBox1" Text="OriginText" runat="server" onblur="setBlur();" onchange="setChange();"></asp:TextBox>

    Or it might be some other reason which need you share us more code.

    Best Regard,

    Yang Shen

    Friday, August 2, 2019 5:11 AM
  • User1510859543 posted

    It must be somewhere else in the page causing the problem as this is done in other pages without issue. Thanks.

    Thursday, August 15, 2019 11:40 AM