There is a button. I want to detect whether the user has clicked on it or he pressed enter, and do something corresponding to which event occurred. However, when I press enter, both the click and the enter handlers are ecexuted.
How can I solve this problem?
HTML:
<button type="button">
Click me
</button>
Jquery:
$("button").keydown(function(e){
var keyCode = e.keyCode || e.which;
if(keyCode === 13){
$("button").after("<div> enter was pressed </div>");
}
})
$("button").click(function(){
$("button").after("<div> click event </div>");
})
Working fiddle
Use
keyPress
instead :Hope this helps.