In part 1 we looked at the HTML elements themselves, what they’re for and how they work.
In this part we’ll take a look at the JS side of forms.
Forms are special
In JS forms are part of a JS collection called forms and accessed through document.forms
. To select a specific form on a page you can do so with either it’s place on it’s page (the first form will be numbered 0
), or by using it’s HTML name
attribute.
<form name="my-form">
//
</form>
So if this was the second form on the page in JS you could access it like so:
const myForm = document.forms.[1];
or:
const myForm = document.forms.my-form;