I have a ASP.NET page which has a form on it. It also has a search form on it. The search is in the top-right of the page, so the button for the search is the first added to the control hierachy.
When you're working on the other form and press enter it will click on the search button. I don't want this, I would prefer that when you press enter the button for the form is clicked.
I tried using jQuery like this:
$('#textBoxId').keyup(function(e) {
if(e.keyCode === 13) {
$('#buttonId').click();
e.preventDefault();
}
});
But the form submitted, meaning that the client-side validation didn't run (ie, the onclick event handler wasn't run on the button).
What's the best way to achieve this?
Try this instead.
$('#textBoxId').keydown(function(e) {
if(e.keyCode === 13) {
e.preventDefault();
$('#buttonId').trigger('click');
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With