Solved

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

Posted on 2013-01-09
1
254 Views
Last Modified: 2013-01-14
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
Comment
Question by:KeithMcElroy
1 Comment
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
Comment Utility
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 Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now