Vanilla JavaScript get all elements in a form

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
Search for a command to run...

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
No comments yet. Be the first to comment.
Most of you know me for my consistency, a golden arrow in my blog series. I've written 1000 articles in 1008 days! Almost an article a day, and my honeymoon was the only holiday I ever took. I'm super proud of this achievement; it has been a fantasti...

It's not the first time I'll be talking about community. I think it's an essential aspect of any successful tool. This shows in my previous explorations of Astro, Medusa, and now Vendure as well. All these products thrive in a super open, welcoming, ...

The cool part about Vendure is how easy it is to set up and how abstract each layer is. Basically, we get the following elements: External database Server Worker Admin UI Frontend While this is amazing, it also brings a bit of complexity when it co...

The previous article looked at customizing Vendure on a data and process level. In this article, we'll look at customizing emails, as they are often a big part of a webshop system. We'll be looking at two different layers of customization for customi...

Even though Vendure is a pretty significant project out of the box, in some cases, we might want to go in and modify some elements to work to our specific use case. In this article, I'll take a high-level look at some elements we can customize within...

If you ever made your own validation you will understand the struggle of getting all the form elements.
I've made code that would loop over each type of input as such:
types = ['input', 'select', 'texture'];
// Manually loop and get all those
That will work, but it's very easy to miss one, and not really maintainable.
Did you know there is a simpler way to retrieve all the elements inside a form?
So let's say we have a form with all kinds of inputs like this:
<form id="form">
<div class="container">
<div class="row">
<label for="firstname">Firstname</label>
<input type="text" name="firstname" id="firstname" />
</div>
<div class="row">
<label for="email">Email</label>
<input type="email" name="email" id="email" />
</div>
<div class="row">
<label for="select">Select</label>
<select name="select" id="select">
<option value="1">Option 1</option>
<option value="2">Option 2</option>
</select>
</div>
<div class="row">
<p>Do you agree?</p>
<label>
<input type="radio" name="agree" value="yes">
Yes
</label>
<label>
<input type="radio" name="agree" value="no">
No
</label>
</div>
<div class="row">
<p>Your favorite animal?</p>
<label>
<input type="checkbox" name="agree" value="penguin">
🐧
</label>
<label>
<input type="checkbox" name="agree" value="dog">
🐶
</label>
</div>
</div>
</div>
</form>
This is a typical form, it has some regular inputs, some select elements, checkboxes, and radio groups.
It also has random markup in between to style your form, see the divs and labels.
So how can we distinguish these elements?
First, let's define a variable that will get our form.
const form = document.getElementById('form');
Now it is literally as simple as calling .elements on this const.
console.log(form.elements);
This will give us an HTMLFormControlsCollection which looks as follows.

As you can see these holds are our form elements, which is already super useful.
You can then loop over them using a forEach loop for instance.
[...form.elements].forEach(item => {
console.log(item);
});
Now it's up to you to create your own validation with this.
You can find this full demo on the following Codepen.
Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter