'JQuery get table row values from input and save to array
I have a table with input fields inside the td. How can i get the values from the input fields (by button click) and store them into an array (one row) using JQuery ? So there are different rows with different contexts and i would like to process row by row, so i have to identify those rows by class and so on.
Thanks!
Update: I would like to have values from test1
in an array, test2
in an array and so on.
<tr class="test1">
<td>
<input type="text" name="test">
<input type="text" name="test">
<input type="text" name="test">
</td>
</tr>
<tr class="test2">
<td>
<input type="text" name="test">
<input type="text" name="test">
<input type="text" name="test">
</td>
</tr>
Solution 1:[1]
$('#button').click(function () {
var array = [];
$('#table tr').each(function () {
var values = [];
$(this)
.find('input')
.each(function () {
values.push(this.val());
});
array.push(values);
});
});
then you can take items by array.pop();
Solution 2:[2]
you can do this way:
HTML:
<table>
<tr class="test1">
<td>
<input type="text" name="test" />
<input type="text" name="test" />
<input type="text" name="test" />
<td>
<input type="button" class="GetValues" value="GetValues" />
</td>
</td>
</tr>
<tr class="test2">
<td>
<input type="text" name="test" />
<input type="text" name="test" />
<input type="text" name="test" />
</td>
<td>
<input type="button" class="GetValues" value="GetValues" />
</td>
</tr>
</table>
JQUERY:
$(".GetValues").click(function () {
var test1 = $(this).closest(".test1").find("input:text").map(function () {
return $(this).val();
});
console.log(test1)
})
FIDDLE:
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | Ehsan Sajjad |