how to get values from html input array using javascript?

05-Nov-2020

.

Admin

Hi Guys,

In this example,I will learn you how to get values from html input array using javascript.you can easy and simply get values from html input array using javascript.

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[]');

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: #af1bf9;">

NiceSnippets

</h1>

<h3 id="mv">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="ns()">

Submit

</button>

</form>

<br>

<p id="var"></p>

<script type="text/javascript">

var v = "The respective values are :";

function ns() {

var input = document.getElementsByName('array[]');

for (var i = 0; i < input.length; i++) {

var p = input[i];

v = v + "array[" + i + "].value= "

+ p.value + " ";

}

document.getElementById("var").innerHTML = v;

document.getElementById("mv").innerHTML = "Output";

}

</script>

</body>

</html>

It will help you...

#JavaScript