Wednesday, December 9, 2015

Explicitly keep a button from submitting a form.

There are a couple of ways to do this:

  1. <button id="button" onclick="return false;">Go</button>
  2. <button id="button" type="button">Go</button>

 
 

I found the second approach of explicitly denoting a button type of "button" to be a little alarming. Here I had to explicitly go in another direction (there is also type="reset" which keeps the form from posting while clearing values out of form fields) and that begs the question: What is the default button type? This suggests a discrepancy between IE and other browsers and buttons in forms and those that are not so, and also suggests that one should always explicitly specify a button type at a button.

No comments:

Post a Comment