I need to make changes to the DOM. I need to present the table content using paragaraphs or something like that. For that I need to get the data per each row of table. How can I do this?
HTML example:
<table style="width:300px">
<tr>
<td>Jill</td>
<td>Smith</td>
<td>50</td>
</tr>
<tr>
<td>Eve</td>
<td>Jackson</td>
<td>94</td>
</tr>
</table>
Output should be something like this - Jill Smith 50
and Eve Jackson 94
I have written something like this, but it goes through all the <td>
tags in the web page. Not row wise.
$("table").each(function() {
$("tr").each(function(){
$("td").each(function(){
label = $('td').text();
});
alert(label);
});
You can use each() to iterate through rows and then cells
Live Demo
$("table tr").each(function(){
text = "";
$(this).find('td').each(function(){
text += $(this).text() + " ";
});
alert(text);
});
Try,
var groupedVales = [];
$('table tr').each(function(){
groupedVales[groupedVales.length] =
$(this).find('td').map(function(){ $(this).text(); }).get().join(' ');
});
Now the groupedvales
contains elements which will be grouped as per your expectations
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With