user2839497 user2839497 - 1 year ago 62
HTML Question

If the type attribute in <button> tag is missing then will that button created with <button> tag behave like a submit button?

Suppose, I have following HTML code :

<button>Click Me!</button>

In above code I have not used the attribute 'type'.

So, in this case will this button behave like a Submit Button which will submit all the form data to the server?

If yes, then can I execute JavaScript code on the onclick event of above button which is missing the 'type' attribute?

If no, what exactly will happen?


Answer Source

If you want a button to behave like just a button, then yes, your markup needs to be:

<button type="button">Click Me!</button>

In this case, you can easily add a click event with:


N.B. If you don't mark up the button properly (ie. you omit the type attribute) and the browser then treats it like a input type="submit", you can still add a submit event:

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download