Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery and ASP.NET - forcing button click

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?

like image 992
Aaron Powell Avatar asked Jan 24 '23 21:01

Aaron Powell


1 Answers

Try this instead.

$('#textBoxId').keydown(function(e) {
  if(e.keyCode === 13) {
    e.preventDefault(); 
    $('#buttonId').trigger('click');
  }
});
like image 104
tvanfosson Avatar answered Jan 26 '23 10:01

tvanfosson