How to get values from html input array using JavaScript ? - GeeksforGeeks

30 Jul, 2021
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:
JavaScript is best known for web page development but it is also used in a variety of non-browser environments. You can learn JavaScript from the ground up by following this JavaScript Tutorial and JavaScript Examples.
HTML is the foundation of webpages, is used for webpage development by structuring websites and web apps.You can learn HTML from the ground up by following this HTML Tutorial and HTML Examples.
CSS is the foundation of webpages, is used for webpage development by styling websites and web apps.You can learn CSS from the ground up by following this CSS Tutorial and CSS Examples.
CSS-Misc
HTML-Misc
Picked
CSS
HTML
JavaScript
Web Technologies
Web technologies Questions
HTML
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Create a Responsive Navbar using ReactJS
Design a web page using HTML and CSS
Form validation using jQuery
How to place text on image using HTML and CSS?
How to auto-resize an image to fit a div container using CSS?
How to set the default value for an HTML <select> element ?
Hide or show elements in HTML using display property
How to Insert Form Data into Database using PHP ?
REST API (Introduction)
Design a web page using HTML and CSS