Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Html Button that calls JQuery and does not post back

Tags:

html

jquery

Is it possible to have a button that doesn't post back to the server, and the click event can be detected by jquery? I have an MVC page that takes a while to load and access a database and i just want to reduce the number of loads required and db hits.

like image 414
John Stuart Avatar asked Dec 21 '22 21:12

John Stuart


1 Answers

You can have attach any behavior you want using jQuery.

Here's your button

<button class="my-button">click this</button>

Here's your jQuery

$("button.my-button").click(function(){
  // do something!
});

If you'd like to use the submit button on the form, here's a more unobtrusive way:

$("#some-form input:submit").click(function(){
  // do something special!

  // prevent default behavior of button
  return false;
});
like image 128
maček Avatar answered Dec 24 '22 11:12

maček