I need to detect when someone hits “enter” in text inputs with a specific class.
My jQuery is as follows:
$('input.large').keypress(function (e) {
if(e.which ==13)
console.log("pressed enter");
});
My HTML is something like this:
<tr><td> Personal Name </td></tr>
<tr><td> <input type='text' class='large'> </td></tr>
<tr><td> Email</td></tr>
<tr><td> <input type='text' class='large'> </td></tr>
When I’ve given the fields IDs and tried $('#elementid').keypress it worked. But this isn’t working. I get no console output. What am I missing?
You can use live (.on()) events in
documentwithkeydown(I think this is better). It’ll allow you detectkeydownin current and future elements that matches with a selector.HTML:
JS (jQuery 1.7+):
Note:
.which,.code,.charCodeand.keyCodeis now deprecated. Use the following new solution:jsFiddle: http://jsfiddle.net/david_proweb/fjgvhubn/2/