Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Capture an Enter Key Pressed anywhere on the page

Tags:

jquery

I need to capture an Enter Key press at any time anywhere on a logon page. It will initiate a logon attempt.

Using jQuery, how would I accomplish this? And would I link it to the body tag?

like image 963
Adam Avatar asked Aug 10 '11 02:08

Adam


People also ask

How do you detect enter press?

To check whether user pressed ENTER key on webpage or on any input element, you can bind keypress or keydown event to that element or document object itself. Then in bind() function check the keycode of pressed key whether it's value is 13 is not.

How do you trigger button click on enter?

To trigger a click button on ENTER key, We can use any of the keyup(), keydown() and keypress() events of jQuery. keyup(): This event occurs when a keyboard key is released. The method either triggers the keyup event, or to run a function when a keyup event occurs.

How do you check if key pressed is Enter key?

To check if an “enter” key is pressed inside a textbox, just bind the keypress() to the textbox. $('#textbox'). keypress(function(event){ var keycode = (event.


2 Answers

$(document).keypress(function(e) {   if(e.which == 13) {     // enter pressed   } }); 
like image 67
sje397 Avatar answered Oct 12 '22 08:10

sje397


The keydown event is fired when a key is pressed down.
The keypress event is fired when a key is pressed down and that key normally produces a character value.

$(document).keydown( function(event) {   if (event.which === 13) {     // login code here   } });  
like image 31
canoe Avatar answered Oct 12 '22 09:10

canoe