Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery Input Event does not Fire if Input is Empty

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.

like image 722
Eric J. Avatar asked Apr 04 '12 03:04

Eric J.


1 Answers

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
});
like image 140
Kristian Avatar answered Oct 01 '22 17:10

Kristian