View RSS Feed

richyrich

Preventing Form Submit with button element in Firefox

Rate this Entry
by on March 11th, 2009 at 10:17 AM (2541 Views)
I had an issue in Firefox where I wanted a <button> element to fire a Javascript function onclick.

However, I found in Firefox that the button also performed a submit on the form, which I didn't want.

After a little investigation I discovered that the default type for a button element is submit. Apparently a bug in IE prevents it from firing.

So, how to fix?

Add a type="button" property to your <button> element

Code:
<button id="button1" type="button" onclick="JS_Function()">Click</button>
I would also include return false as the last line of your JS function, just to be on the safe side.

Submit "Preventing Form Submit with button element in Firefox" to Digg Submit "Preventing Form Submit with button element in Firefox" to del.icio.us Submit "Preventing Form Submit with button element in Firefox" to StumbleUpon Submit "Preventing Form Submit with button element in Firefox" to Google

Updated March 11th, 2009 at 10:19 AM by richyrich

Categories
Programming & Scripting , JavaScript , Web Design , HTML/XHTML

Comments


SEO by vBSEO