Reputation: 43
I have 100 inputs with the name table[]
. How can I get their value with jQuery, as an array?
I am trying to do something like $_POST['table']
in PHP.
I tried the following code, but I want the values as an array...
$("input[name='table[]']").each(function(){document.write($(this).val());});
Upvotes: 3
Views: 2259
Reputation: 2994
Following code gives the value of each input
var arr=new Array();
$("input[name='table[]']").each(function()
{
var val=$(this).attr('value');
document.write(val);
arr.Push(val);
});
Upvotes: 3
Reputation: 187110
var arrInputValues = new Array();
$("input[name='table\\[\\]']").each(function(){
arrInputValues.push($(this).val());
// you can also use this
//arrInputValues.push(this.value);
});
Now your arrInputValues contains all the value.
You can also use
$("input[name='table[]']").each(function(){
You can see a working demo
You can use join() method to join the values in the array.
arrInputValues.join(',');
will join the array elements as a string separated by ,
.
Upvotes: 5
Reputation: 6062
the [ and ] characters are special characters, you need to escape them
$("input[name='table\\[\\]']").each(function()
{ document.write($(this).val()); });
Upvotes: 2
Reputation: 382909
You need to escape the []
chars, try this:
$("input[name='table\\[\\]']").each(function()
...........
Upvotes: 2