To access form elements by HTML ID or name attribute with JavaScript, we use the elements
property.
For instance, we write
<form id="myForm">
<input type="text" name="foo" />
</form>
to add a form.
Then we write
const fooInput = document.getElementById("myForm").elements["foo"];
to get the input inside the form.
We select the form with getElementById
.
And then we get the input with the elements["foo"]
property.
'foo'
is the name attribute value of the input.