jQuery actually has built in key event handlers:
http://jquery.com/api/

Click the K link at the top 

-----Original Message-----
From: jquery-en@googlegroups.com [mailto:[EMAIL PROTECTED] On
Behalf Of JimD
Sent: Monday, June 11, 2007 11:48 AM
To: jQuery (English)
Subject: [jQuery] keycode javascript conversion to jquery


Hi all,

I'm looking to convert an old javascript function to jquery. It simple
captured the keycode event 13 (user pressing enter on the keyboard) if the
user hits enter it initiates the action of clicking a button. I want to try
jquery because no matter what I do with regular javascript, the keycode
never get properly initiated in firefox. I was wondering if any of you could
suggest the best method using jquery so it works with both firefox 2 and
IE6/7

Current javascript:

/* for enter key to initiate simple search clicking enter key */ function
DoEnterSearch(buttonName,e)  {
    var key;

    var key = (e)? e.which: event.keyCode;

    if (key == 13)
    {
        //Get the button the user wants to have clicked
        var btn = document.getElementById(buttonName);
        if (btn != null)
        { //If we find the button click it
            btn.click();
            if (event) event.keyCode = 0 ;
            return false
        }
    }
}


Jquery implementation

$(document).ready(function() {
  $("#searchtxt").keydown(
     function(e){
       var key = e.charCode || e.keyCode || 0;
       $('buttonsmp').click();
     }
  );
});


Reply via email to