I am having a problem with keypresses registering the event twice. I am currently using .live because the HTML elements are dynamically loaded and changing frequently.
I did try using keydown as well as keypress, but the result was no different. Anyone able to spot what is causing them to register twice?
The Javascript:
$(".class3").live('keypress', function (e) {
if ( e.keyCode == 13 ){
alert("Enter key pressed");
}
});
The HTML:
<dd class="class1 class2">
<input type="text" class="class2 class3" placeholder="Type Name Here">
</dd>
You have not pasted your code in here correctly. What you are trying to do is this:
http://jsfiddle.net/pnNAM/
The reason this is happening, is because your
keypressevent is bubbling up. So it fires once for theinput, then again for thedd.If you’ll indeed only listen to
keypresson$(".class3"), it’ll only fire once.