how to get values from html input array using javascript?
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...
- How to set Date in setUTCHours() Method in JavaScript?
- How to Date sets UTC milliseconds in Javascript?
- How to Use push(), unshift(),pop() and shift() With Array in Javascript?
- How to Check if a Variable is a Number in Javascript?
- How To Format a Number Using Fixed - Point Notation in Javascript?
- How to Insert an Element into an Array in javaScript?
- How to remove duplicate elements from JavaScript Array?
- How to calculate years, months and days between two dates in javascript?