Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does a button cause my whole web page to reload?

I have a web page I'm working on with jQuery. I'm getting erratic behavior from some elements on my page: Every time a button, any button, on the page is clicked, the page refreshes.

The page must somehow be running some code that reloads that page any time a button is clicked.

I'm completely stumped trying to figure out where the code is getting bound to the click handler, so I would like to know if it is possible to enumerate, at run-time, a list of handlers attached to a button.

Update: After reading the answers given below, I changed a line in my page:

<button id="btnSaveAndContinue" class="button" tabindex="290">Save and Continue</button>

to this:

<input type='button' id="btnSaveAndContinue" class="button" tabindex="290" value='Save and Continue' />
like image 530
Vivian River Avatar asked Jun 11 '12 18:06

Vivian River


1 Answers

This is the default behaviour of a button. If you want to change it, do something like this:

$("button selector").click( function(event) {
  event.preventDefault();
});
like image 124
robbrit Avatar answered Oct 01 '22 00:10

robbrit