Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to loop through elements of forms with JavaScript?

I have a form:

<form method="POST" action="submit.php">     <input type="text" value="1">     <input type="text" value="2">     <input type="text" value="3">     <input type="text" value="4">     <button type="submit">Submit</button> </form> 

How can I loop over the input elements in the form (in order to perform some validation on them)?

I'd prefer to use only pure JavaScript, not jQuery or another library. I'd also like to limit the iteration to form elements, not any other elements which may be added to the form.

like image 311
Please Delete me Avatar asked Nov 14 '13 13:11

Please Delete me


People also ask

How do you iterate through an element in JavaScript?

After selecting elements using the querySelectorAll() or getElementsByTagName() , you will get a collection of elements as a NodeList . To iterate over the selected elements, you can use forEach() method (supported by most modern web browsers, not IE) or just use the plain old for-loop.

Can you loop through an array in JavaScript?

If we want to loop through an array, we can use the length property to specify that the loop should continue until we reach the last element of our array.

Can you use JavaScript for forms?

With JavaScript at your side, you can process simple forms without invoking the server. And when submitting the form to a CGI program is necessary, you can have JavaScript take care of all the preliminary requirements, such as validating input to ensure that the user has dotted every i.

What is the JavaScript method to loop through every item of an array?

The JavaScript Array forEach Method. The traditional for loop is easy to understand, but sometimes the syntax can be tedious. For example, the nested loop requires new variable declarations with a nested scope. Modern JavaScript has added a forEach method to the native array object.


2 Answers

You need to get a reference of your form, and after that you can iterate the elements collection. So, assuming for instance:

<form method="POST" action="submit.php" id="my-form">   ..etc.. </form> 

You will have something like:

var elements = document.getElementById("my-form").elements;  for (var i = 0, element; element = elements[i++];) {     if (element.type === "text" && element.value === "")         console.log("it's an empty textfield") } 

Notice that in browser that would support querySelectorAll you can also do something like:

var elements = document.querySelectorAll("#my-form input[type=text][value='']") 

And you will have in elements just the element that have an empty value attribute. Notice however that if the value is changed by the user, the attribute will be remain the same, so this code is only to filter by attribute not by the object's property. Of course, you can also mix the two solution:

var elements = document.querySelectorAll("#my-form input[type=text]")  for (var i = 0, element; element = elements[i++];) {     if (element.value === "")         console.log("it's an empty textfield") } 

You will basically save one check.

like image 54
ZER0 Avatar answered Oct 14 '22 10:10

ZER0


A modern ES6 approach. Select the form with any method you like. Use the spread operator to convert HTMLFormControlsCollection to an Array, then the forEach method is available. [...form.elements].forEach

Update: Array.from is a nicer alternative to spread Array.from(form.elements) it's slightly clearer behaviour.


An example below iterates over every input in the form. You can filter out certain input types by checking input.type != "submit"

const forms = document.querySelectorAll('form'); const form = forms[0];  Array.from(form.elements).forEach((input) => {   console.log(input); });
<div>   <h1>Input Form Selection</h1>   <form>     <label>       Foo       <input type="text" placeholder="Foo" name="Foo" />     </label>     <label>       Password       <input type="password" placeholder="Password" />     </label>     <label>       Foo       <input type="text" placeholder="Bar" name="Bar" />     </label>     <span>Ts &amp; Cs</span>     <input type="hidden" name="_id" />     <input type="submit" name="_id" />   </form> </div>
like image 40
Lex Avatar answered Oct 14 '22 10:10

Lex