I found a behavior that I don't understand while debugging my JavaScript code.
By having a form
element in my HTML, the window.submit
JavaScript variable is being automatically populated with a reference to the form's submit button.
I am unfamiliar with this behavior and tried my best to google why the form element is defining the JavaScript variable. Is there part of the HTML spec that describes this behavior?
Live demo - you can see that the variable starts out as undefined
, then after the form
in the HTML, the variable points to the form's submit button. Why?
<script>
console.log(window.submit); // Shows "undefined" in console
</script>
<form id="form">
<label for="message">Message:</label>
<input type="text" id="message">
<br>
<button type="submit" id="submit">Send</button>
</form>
<script>
console.log(window.submit); // Shows the above "button" element in console
</script>