[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 269
  • Last Modified:

javascript or jquery enter key identify which field in class was in focus when enter key pressed

Not able to get this to work.  this.id and $(this).attr("id") each return undefined.
How can I make this work so that I return the id of the field that is being clicked?

Sample A:  Desired solution
$(document).keypress(function(event){
 
      var keycode = (event.keyCode ? event.keyCode : event.which);
      if(keycode == '13'){
            alert('You pressed a "enter" key in somewhere ' + this.id);      
      }
 
});            

Sample B more desired solution
$('.myclass').keypress(function(event){
 
      var keycode = (event.keyCode ? event.keyCode : event.which);
      if(keycode == '13'){
            alert('You pressed a "enter" key in somewhere ' + this.id);      
      }
 
});
0
KeithMcElroy
Asked:
KeithMcElroy
1 Solution
 
leakim971PluritechnicianCommented:
http://api.jquery.com/event.which/
the event.which property normalizes event.keyCode and event.charCode
check this : http://jsfiddle.net/UbG4W/
$('.myclass').keypress(function(event){
      if(event.which == '13'){
            alert('You pressed a "enter" key in somewhere ' + this.id);      
      }
});

Open in new window

0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now