Why can't the HTML form be submitted by a button named "submit"?

I am trying to submit an HTML form,

<form action="go.php">
    <input id="I" type="button" name="submit" value="Go" onclick="this.form.submit()"/>


The code above failed, but if I change the name of the button to something else like

<form action="go.php">
    <input id="I" type="button" name="bt" value="Go" onclick="this.form.submit()"/>


it works. What for? What's the difference between the two?


source to share

1 answer

Interactive elements that are on a form and have "name" attributes are associated in the DOM graph with the DOM node form as node properties. Property names are taken from the "name" attributes on the interactive elements.

Thus, if you look at the node form through the developer console, you will see properties corresponding to your inputs, buttons, text boxes, selections, etc. So, given:

<form id=myform>
  <input name=sometext value="hello world">


then this will work with JavaScript:

var f = document.getElementById("myform");
alert(f.sometext.value); // "hello world"


This strange behavior causes problems when you use names that clash with other useful properties in the DOM node form, such as "submit". The link to your button named "submit" has overridden the link to the "submit" function that was there when the node form was "born".



All Articles