user2839497 user2839497 - 6 days ago 5
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?

Thanks.

Answer

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:

document.getElementsByTagName('button')[0].addEventListener('click',myFunction,false);

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:

document.getElementsByTagName('button')[0].addEventListener('submit',myFunction,false);
Comments