How do I store table cell values in a jquery array?
-
7This question is poorly worded. Can you explain what you mean, what you have, what you want?Konerak– Konerak2011-08-22 08:49:16 +00:00Commented Aug 22, 2011 at 8:49
-
2Maybe also tell us how you want to retrieve the values. I'm guessing you'll want to store the values in a 2-dimensional array (rows and columns) so that you could use a function such as getTableDatafrom(row, column) to get the value of a specific <td> in a specific <tr>.T. Junghans– T. Junghans2011-08-22 09:09:58 +00:00Commented Aug 22, 2011 at 9:09
Add a comment
|
4 Answers
iterate over each table cell and push its value in the array say for example you have table structure like
<table>
<tr>
<td>improve your</td>
</tr>
<tr>
<td>accept rate plz!</td>
</tr>
</table>
you can do
var arr=[];
$("td").each(function(){
arr.push($(this).text());
});
$.each(arr,function(index,value){
alert(arr[index]);
});
here is the fiddle http://jsfiddle.net/qNgST/1/
Comments
// USE: var myTableArray = table2array( $('#idTable') );
function table2array( jTable ) {
tableArr = new Array();
var ix = 0;
$(jTable).find('tr').each(function(){
tableArr[ix] = new Array();
$(this).find('th, td').each(function(){
tableArr[ix].push($(this).text().trim());
});
ix++;
});
return tableArr;
};