Spiderman
Spiderman

Reputation: 10030

button in the form cause it be submitted automatically

has the HTML version changed lately (like from ie7 to IE8?) I notice the following change that cause me some troulbe - I am having a code that is similar to this:

<form method="POST" action="/admin/modify">
<input type="text"/>
<button onclick="dosomething()">Press</button>
</form>
<script type="text/javascript">
function doSomething(){
// doesn't matter what actually
}
</script>

What is weired to me in this code is that by pressing the button inside the form, all I want is to perform some javascript action but eventaully it causes the form to be submitted too, even when I am not willing to do it.
So - is it true? and if so how can I perform some java script actoin inside a from but prevent the form from being submitted automatically?

Upvotes: 29

Views: 9339

Answers (2)

Pekka
Pekka

Reputation: 449475

According to W3schools, submit is the new default action for button elements in IE 8:

Always specify the type attribute for the button. The default type for Internet Explorer is "button", while in other browsers (and in the W3C specification) it is "submit".

So if you don't specify a type, the form will be submitted in all browsers, but not IE 7.

this should work:

<button type="button" onclick="dosomething()">Press</button>

Upvotes: 48

Andy E
Andy E

Reputation: 344575

From the IE docs for the button element:

Internet Explorer 8 and later. The default value of this attribute depends on the current document compatibility mode. In IE8 mode, the default value is submit. In other compatibility modes and earlier versions of Internet Explorer, the default value is button.

The standard suggests the default type is submit, previous versions of IE incorrectly defaulted to button

Upvotes: 2

Related Questions