On my forms I prevent the user from submitting the form by clicking enter in text elements however, I want to allow them to use the enter key to submit the form when the button element has focus. Currently, it only works with the spacebar. I’ve tried the following:
$("#dyn-gen-form").on("keypress", "#genAttFormBtn", function(e) {
e.preventDefault();
log("pressed a key");
var k = e.keyCode || e.which;
if (k == 13) {
// do the work
return false;
}
});
But it isn’t working. By isn’t working, I mean, the event isn’t triggered. I get nothing.
I made a sample for you: http://jsfiddle.net/p7vzJ/54/ – button detects spacebar as well as enter key stroke; Please press space or enter after the jsfiddle is loaded.
I can remove my post if you don’t wanna use
.livePlease let me know.JQuery Code
cheers!