I've got a form like this:
<form>
<input type="text" name="name" value="object name"><br>
<input type="text" name="atrib[]" value="atrib name"><input type="text" name="val[]" value="default value"><br>
<input type="text" name="atrib[]" value="atrib name"><input type="text" name="val[]" value="default value"><br>
<input type="text" name="atrib[]" value="atrib name"><input type="text" name="val[]" value="default value"><br>
<div id="fooBar"></div>
<input type="button" value="Add" onclick="add();"><input type="button" value="Generate" onclick="gen();">
</form>
With some JavaScript like this:
<script>
function add() {
//Create an input type dynamically.
var element = document.createElement("input");
var element2 = document.createElement("input");
//Assign different attributes to the element.
element.setAttribute("type", "text");
element.setAttribute("value", "atrib name");
element.setAttribute("name", "atrib[]");
element2.setAttribute("type", "text");
element2.setAttribute("value", "default value");
element2.setAttribute("name", "val[]");
// the div id, where new fields are to be added
var bar = document.getElementById("bar");
//Append the element in page (in span).
bar.appendChild(element);
bar.appendChild(element2);
bar.innerHTML += "<br>";
}
function gen() {
var inputAtrib = document.getElementsByName("atrib[]").value;
var inputVal = document.getElementsByName("val[]").value;
alert(inputAtrib);
alert(inputVal);
}
</script>
What I need to do is retrieve both atrib[] and val[] arrays when a user clicks on generate onclick="gen();" button in order to loop trough them and perform some operations. When trying to do so, inputAtrib and inputVal return undefined. I wasn't able to find an easy answer for this, please, could you help me to find the right road to the solution of this problem?
document.getElementsByName("atrib").valuewithout any luck, it returnsundefinedatribbutatrib[]atrib[]returns undefined as well. Also, I don't understand why the downvote. I've done my research and found nothing about this. I found a lot of useful information for retrieving data from normal inputs, but anything about arrays >.<