This problem can be solved by using input tags having the same “name” attribute value that can group multiple values stored under one name which could later be accessed by using that name. To access all the values entered in input fields use the following method:
var input = document.getElementsByName('array[]');
The document.getElementsByName() method is used to return all the values stored under a particular name and thus making input variable an array indexed from 0 to number of inputs. The method document.getElementById().innerHTML is used to change the inner HTML of selected Id. So in this way, we can access the input values and convert them into the array and access it as you like. We have used form tag because it is generally a good practice to group all the input elements together inside a form otherwise in the present scenario it is completely optional.
Example:
<!DOCTYPE html> <html lang="en" dir="ltr"> <head> <meta charset="utf-8"> <title> How to get values from html input array using JavaScript ? </title> </head> <body style="text-align: center;"> <h1 style="color: green;"> GeeksforGeeks </h1> <h3 id="po">Input Array Elements</h3> <form class="" action="index.html" method="post"> <input type="text" name="array[]" value="" /><br> <input type="text" name="array[]" value="" /><br> <input type="text" name="array[]" value="" /><br> <input type="text" name="array[]" value="" /><br> <input type="text" name="array[]" value="" /><br> <button type="button" name="button" onclick="Geeks()"> Submit </button> </form> <br> <p id="par"></p> <script type="text/javascript"> var k = "The respective values are :"; function Geeks() { var input = document.getElementsByName('array[]'); for (var i = 0; i < input.length; i++) { var a = input[i]; k = k + "array[" + i + "].value= " + a.value + " "; } document.getElementById("par").innerHTML = k; document.getElementById("po").innerHTML = "Output"; } </script> </body> </html> |
Output:
- Input Array Elements:

- Output Array Elements:

Recommended Posts:
- How to get all unique values (remove duplicates) in a JavaScript array?
- JavaScript | Get all non-unique values from an array
- How to get the value of text input field using JavaScript?
- How to get the javascript function parameter names/values dynamically?
- How to get all property values of a JavaScript Object (without knowing the keys) ?
- How to get current formatted date dd/mm/yyyy in Javascript and append it to an input?
- How to select all text in HTML text input when clicked using JavaScript?
- How to remove “disabled” attribute from HTML input element using JavaScript ?
- How to validate if input date (end date) in input field must be after a given date (start date) using express-validator ?
- How to validate if input in input field exactly equals to some other value using express-validator ?
- How to validate if input in input field has alphanumeric characters only using express-validator ?
- How to validate if input in input field has base64 encoded string using express-validator ?
- How to validate if input in input field has boolean value using express-validator ?
- How to validate if input in input field has ASCII characters using express-validator ?
- How to validate if input in input field has base 32 encoded string using express-validator ?
- How to validate if input in input field has alphabets only using express-validator ?
- How to validate if input in input field has BIC or Swift code only using express-validator ?
- How to validate if input in input field has integer number only using express-validator ?
- How to validate if input date (start date) in input field must be before a given date (end date) using express-validator ?
- How to validate if input in input field has decimal number only using express-validator ?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


