JQuery Event for user pressing enter in a textbox?

Click Upvote picture Click Upvote · Jun 29, 2011 · Viewed 332.5k times · Source

Is there any event in Jquery that's triggered only if the user hits the enter button in a textbox? Or any plugin that can be added to include this? If not, how would I write a quick plugin that would do this?

Answer

Jishnu A P picture Jishnu A P · Jun 29, 2011

You can wire up your own custom event

$('textarea').bind("enterKey",function(e){
   //do stuff here
});
$('textarea').keyup(function(e){
    if(e.keyCode == 13)
    {
        $(this).trigger("enterKey");
    }
});

http://jsfiddle.net/x7HVQ/