We can reference the fields by name or ID:
const contactForm = document.querySelector("#contact-form");
// Reference by name or ID
const nameInput = contactForm.elements.name;
const emailInput = contactForm.elements.email;
const messageTextArea = contactForm.elements.message;
We can also reference the fields by index. This lets us reference fields that don't have a name or ID, such as the submit button:
const contactForm = document.querySelector("#contact-form");
// Reference by index
const nameInput = contactForm.elements[0];
const emailInput = contactForm.elements[1];
const messageTextArea = contactForm.elements[2];
const submitButton = contactForm.elements[3];
The following elements are included by the .elements
property:
<button>
<fieldset>
<input>
(but not type="image"
🤷)<object>
<output>
<select>
<textarea>
Image inputs aren't included for some weird historical reasons; you can still select these using the .querySelector()
method:
const contactForm = document.querySelector("#contact-form");
const imageInput = contactForm.querySelector("[type='image']");
The HTMLFormElement.elements
property returns an HTMLFormControlsCollection
. If you need to use any array methods on it, you'll need to convert it to an array. I've used the Array.from()
method below, but you could also use spread syntax:
const contactForm = document.querySelector("#contact-form");
// or [ ...contactForm.elements ]
const formFieldsArray = Array.from(contactForm.elements);
The HTMLFormElement.elements
property is supported all the way back to IE 5.5!