jquery Troubles with binding to an element by id

Posted by Alonzo on Stack Overflow See other posts from Stack Overflow or by Alonzo
Published on 2009-09-03T19:18:39Z Indexed on 2010/03/24 18:03 UTC
Read the original article Hit count: 150

Filed under:
|

Hi What im trying to do is that when the esc key is pressed on the text input field (id = txtSearch), some code will run. this is my code:

$(document).ready(function() { 
           $('#txtSearch').bind('keyup', function(e) { 
                var characterCode; // literal character code will be stored in this variable 
                if (e && e.which) { //if which property of event object is supported (NN4) 
                    e = e; 
                    characterCode = e.which; 
                } 
                else { 
                    characterCode = e.keyCode; 
                } 
                if (characterCode == 27)  //if esc key
                { 
                    //do stuff 
                } 
            }); 
}

It doesnt work for me. however, if i would change that line:

$('#txtSearch').bind('keyup', function(e) {

with:

$(document).bind('keyup', function(e) {

everything works. but i dont want to bind the keyup with esc to the whole document... any suggestions on how i can bind the the keyup only with the specific text search element? (or its containing div or something) Thanks!

© Stack Overflow or respective owner

Related posts about jQuery

Related posts about keyboard