Capture an Enter Key Pressed anywhere on the page

Jquery

Jquery Problem Overview


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?

Jquery Solutions


Solution 1 - Jquery

$(document).keypress(function(e) {
  if(e.which == 13) {
    // enter pressed
  }
});

Solution 2 - Jquery

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
  }
}); 

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAdamView Question on Stackoverflow
Solution 1 - Jquerysje397View Answer on Stackoverflow
Solution 2 - JquerycanoeView Answer on Stackoverflow