I’m having problem to get all form array values to JS array, with one value it working fine:
var el = document.getElementsByName('p[]');
for (var i = 0, j = el.length; i < j; i++) {
var elem = el[i];
alert(elem.value);
}
But when I try to add more its not working:
var el = document.getElementsByName('p[]', 'k[]', 'ka[]', 's[]');
for (var i = 0, j = el.length; i < j; i++) {
var elem = el[i];
alert(elem.value);
$.post("test.php", { 'p': [elem.value] });
}
Here is my form: form is dynamic (with button click I can add new line with these 4 fields:
<form name="form">
<input type="text" name="p[]" id=name" />
<input type="text" name="k[]" id="quant" size="3" />
<input type="text" name="ka[]" id="price" size="10" />
<input type="text" name="s[]" id="sum" size="10" disabled="disabled"/><br />
</form>
document.getElementsByNamedoesn’t magically take multiple arguments in the way that you’re trying to use it. You need to callgEBNmultiple times, once for each name (described in more detail by another answer).Alternately, just loop over the
<form>‘s inputs: