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.
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.
Share Improve this question edited Nov 27, 2016 at 11:15 PHPLover asked Nov 27, 2016 at 11:09 PHPLoverPHPLover 13k53 gold badges172 silver badges324 bronze badges 1- 2 why was this down voted this is a legitimate question – Buddhi741 Commented Nov 27, 2016 at 11:16
2 Answers
Reset to default 4If 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);
Yes, the default type
for button
is submit
; as is documented in the spec.
So, in this case will this button behave like a Submit Button which will submit all the form data to the server?
Yes, it will.
If yes, then can I execute JavaScript code on the onclick event of above button which is missing the 'type' attribute?
Yes, of course you can, just like any other button. The click handler will run before the form is submitted.