I just realized that inside a form event handler (like onsubmit
or oninput
etc), you can access control values globally, meaning the following does indeed work:
<form onsubmit="alert(theInput.value); return false">
<input name="theInput">
</form>
Why does this work? I never defined theInput
anywhere and it is also not a global variable.
Assuming that the internal browser code assigns those variables itself, why cant I access theInput
in a custom event handler?
function submitHandler() {
alert(theInput.value)
}
<form onsubmit="submitHandler(); return false">
<input name="theInput">
</form>
In submitHandler()
, theInput
is undefined and the code breaks, as I expected.
Is there any documentation available about this? Could not find any, but it is admittedly something hard to search for. MDN docs even use this syntax in one of their examples.