6

I have a jQuery event handler that reacts to every change in an:

<input id="myId" type="text" /> 

element:

$("#myId").bind('input', function (e) {    
        // Do Stuff
    });

This works perfectly, except when the input in #myId is empty (e.g. there was just 1 character in the input box, and I remove it with Backspace). If the input has become empty, the event does not fire. If I then enter a character, the event fires.

Is this a known issue? Am I doing something wrong? Any way to get an event when the input goes empty?

Update

The event does not fire AT ALL when Backspace is entered, at least in IE9.

3
  • 1
    When is the event supposed to be fired? Can't you use keyup event? Commented Apr 4, 2012 at 3:39
  • The oninput event doesn't seem to be widely used but it appears to exist: developer.mozilla.org/en/DOM/window.oninput Commented Apr 4, 2012 at 3:43
  • 1
    FYI, input event doesn't work in IE6,7,8. Commented Apr 4, 2012 at 3:43

1 Answer 1

9

as far as I know, "input" is not the event you want to use. Since "input" is analogous to "change", it tends to produce a bad result when your string approaches 0 chars, use keyup instead

$("input#myId").bind('keyup', function (e) {    
    // Do Stuff
});
Sign up to request clarification or add additional context in comments.

1 Comment

I could be wrong here, but I don't think you can say keyup is "the event you want to use" because input, unlike keyup, detects ANY input, as in cuts & pastes with the mouse. What we have here is simply that firefox and IE disagree on whether a backspace or delete should count as "input". I can see both sides... My guess is that Eric J. really wants an on change event that'd fire without waiting until the element loses focus like the regular change event does. I dunno what the answer to that question is.

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.