jQuery Input Event does not Fire if Input is Empty

Go To StackoverFlow.com

5

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.

2012-04-04 03:36
by Eric J.
When is the event supposed to be fired? Can't you use keyup event - elclanrs 2012-04-04 03:39
The oninput event doesn't seem to be widely used but it appears to exist: https://developer.mozilla.org/en/DOM/window.oninpu - j08691 2012-04-04 03:43
FYI, input event doesn't work in IE6,7,8 - jfriend00 2012-04-04 03:43


8

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
});
2012-04-04 03:40
by Kristian
https://developer.mozilla.org/en/DOM/window.oninpu - j08691 2012-04-04 03:42
ah, very interesting. thank - Kristian 2012-04-04 03:43
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 - ctb 2013-09-03 17:58
Ads