Search code examples
javascriptonkeydown

How do I write a keyup function


Possible Duplicate:
Run Jquery method when enter key is press

I am trying to write a function that when the user presses the enter key it does the same thing as the code below:

$('#switch-fighter-search-button-link').click(function(){
  window.location=$(this).attr("href")+$('#switch-fighter-text').val();
  return false;
});

How can I accomplish this?


Solution

  • Try this (I am assuming you are using jQuery based on the question contents):

           $(document).keydown(function(objEvent) {
                if (objEvent.keyCode == 13) { //enter is pressed
                    $('#switch-fighter-search-button-link').click();
                }
            })