11

I have a problem with a jquery change event on a text input that works as expected in Firefox and IE but not in Chrome. I also have a keyup event on the text input to manipulate the input as it is entered, but when the input is done I need to run some extra code. I tried using the 'blur' event and the 'focusout' event as someone here had suggested as a substitute, but then I couldn't change focus at all -- the input kept grabbing it back somehow. Here is the code:

 $('.textInput').keyup(function(event) { /* do stuff */ });
 $('.textInput').change(function(event) { alert('Changed!'); /* do other stuff */ });

and the html:

 <input type="text" class="textInput" value="" />

I am using jQuery 1.6.3. Any suggestions would be greatly appreciated. H.

4
  • Works for me in Chromium 14/Ubuntu 11.04. Commented Dec 6, 2011 at 22:30
  • can you post the error that Chrome gives you? If you do Inspect Element (right click on page) it should show you what the error is. Commented Dec 6, 2011 at 22:42
  • What other js code do you have loaded on the page that would make it grab focus back? Change only fires when focus changes, that that's probably why. Commented Dec 6, 2011 at 23:12
  • There is no error, the change event just doesn't fire. Since it works for others, I changed tack and narrowed it down to something in the keyup handler that prevents the change event on Chrome. It is not my code in there so I have worked around it by running the code from the change handler in the keyup handler -- not the most efficient but no major harm done. When I have some time I will try to find the offending code in the keyup handler. Thanks for looking at it. Commented Dec 7, 2011 at 14:13

4 Answers 4

12

The change event will not fire unless the input focus switched to other controls

Sign up to request clarification or add additional context in comments.

Comments

1

You can use the input event that works like a charm:

$('#search-form .term').bind('input', function(){
  console.log('this actually works');
});

Source: https://gist.github.com/brandonaaskov/1596867

Comments

0
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script src="Scripts/jquery-1.7.1.min.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            $('.textInput').keyup(function (event) {
                var obj = $(this);
                if ((event.keyCode ? event.keyCode : event.which) === 13) {
                    $('#result').text('Enter has been triggered.');
                }
            }); ;

            $('.textInput').change(function (event) {
                var obj = $(this);
                $('#result').text('Input text has been changed:' + obj.val());
            }); ;
        });
    </script>
</head>
<body>
    <span id="result"></span>
    <br />
    Enter some text and press enter:
    <input type="text" class="textInput" value="" />
</body>
</html>

Comments

0

Actually, there's a bug on Google Chrome that causes the 'change' event to not fire if something has been changed by the 'keyup' event:

https://code.google.com/p/chromium/issues/detail?id=92492

The issue seems to be open since May 2, 2013.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.