Edward Tanguay Edward Tanguay - 6 months ago 10
Javascript Question

Why does JQuery keydown work for window but not textbox?

Why does this work:

$(window).keydown(function(event){
alert(event.keyCode);
});


but not this:

$('#ajaxSearchText').keydown(function(event){
alert(event.keyCode);
});


I'm testing with Firefox 3.
Interestingly, neither of them work in IE7.

Answer

Checked this in Chrome, IE7 and Firefox 3.0.3. Works as it should. jQuery version 1.2.6.

<html> 
  <head> 
    <script type="text/javascript" src="jquery-1.2.6.js"></script> 
    <script type="text/javascript"> 
      $(function() 
      {
        $("#ajaxSearchText").keydown(function(event)
        {
          alert(event.keyCode);
        });
      });
    </script> 
  </head> 
  <body> 
    <input type="text" id="ajaxSearchText"></input>
  </body> 
</html>