Wednesday, 9 March 2016

javascript - HTML button to NOT submit form



I have a form. Outside that form, I have a button. A simple button, like this:







Nevertheless, when I click it, it submits the form. Here's the code:









All this button should do is some JavaScript. But even when it looks just like in the code above, it submits the form. When I change the tag button to span, it works perfectly. But unfortunately, it needs to be a button. Is there any way to block that button from submitting the form? Like e. g.






Answer



I think this is the most annoying little peculiarity of HTML... That button needs to be of type "button" in order to not submit.






Update 5-Feb-2019: As per the HTML Living Standard (and also HTML 5 specification):





The missing value default and invalid value default are the Submit
Button state.



No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...