Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Acess the table data <td> row by row <tr> individually

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);
 });
like image 573
user3547920 Avatar asked May 28 '14 09:05

user3547920


2 Answers

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);
});
like image 164
Adil Avatar answered Sep 25 '22 13:09

Adil


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

DEMO

like image 20
Rajaprabhu Aravindasamy Avatar answered Sep 23 '22 13:09

Rajaprabhu Aravindasamy