I found this question from before, here's the answer but I can't make it work. So the question is: I want to get all the values from the table into array, using javascript
HTML table:
<table id="cartGrid">
<thead>
<tr>
<th>Item Description</th>
<th>Qty</th>
<th>Unit Price</th>
<th>Ext Price</th>
</tr>
</thead>
<tbody>
<tr><td>Old Lamp</td><td>1</td><td>107.00</td><td>107.00</td>
<tr><td>Blue POst</td><td>2</td><td>7.00</td><td>14.00</td>
</tbody>
</table>
JavaScript:
var myTableArray = [];
$("table#cartGrid tr").each(function() {
var arrayOfThisRow = [];
var tableData = $(this).find('td');
if (tableData.length > 0) {
tableData.each(function() { arrayOfThisRow.push($(this).text()); });
myTableArray.push(arrayOfThisRow);
}
});
alert(myTableArray);
I found another option of doing it - but both return an empty array
var tableData = new Array();
$('#cartGrid tr').each(function(row, tr){
tableData[row]={
"ItemDescription" : $(tr).find('td:eq(0)').text()
, "Qty" :$(tr).find('td:eq(1)').text()
, "UnitPrice" : $(tr).find('td:eq(2)').text()
, "ExtPrice" : $(tr).find('td:eq(3)').text()
}
});
tableData.shift(); // first row is the table header - so remove