I have multiple submit button on the page. What I want is if user presses enter on any input field it should click the related submit button. I tried the way below but it even couldnt catch the key press on the input fields.
Thanks in advance,
$('* input').keypress(function (e) {
if ((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13)) {
$(this).closest('input[type=submit]').click();
}
});
Try this.
.closest()won’t work unless the submit button is located upside in the DOM tree. Instead you could search for it inside the input’s parentNode, or just submit the form (but you probably don’t have a form element, because otherwise this would be the default behavior for the enter key).