senzacionale
senzacionale

Reputation: 20906

jQuery read all TD's table data

With jquery I am reading table:

$('#lc_searchresult > table > tbody > tr').each(function() {
            var data = $(this).find("td:eq(5)").html();
            alert(data);        
        });

it is working fine if TR tag has one TD inside like:

<tr>
<td>654321</td>       
</tr>

but If I am having two TD's then I am geting just first one:

<tr>
<td>654321</td>            
<td>13456</td>        
</tr>

How can I get all of TD's from TR with $(this).find("td:eq(5)").html()

Upvotes: 0

Views: 6002

Answers (5)

Ali Elzoheiry
Ali Elzoheiry

Reputation: 1682

well when you write

var data = $(this).find("td");

the variable data contains an array of all the td's not just the first one

if you say

data.each(function(){
    alert($(this).html())
})  

you will get all the td's

Upvotes: 0

dadarya
dadarya

Reputation: 313

$('#lc_searchresult > table > tbody > tr').each(function() {
  $(this).children('td').each(function(){
    var data = $(this).html();
  alert(data);
  })

});

Upvotes: 2

gaauspawcscwcj
gaauspawcscwcj

Reputation: 3

td:eq(5) : mean you get data of TD with exactly index. so you can't get all data of TR with eq().

Upvotes: 0

Dimag Kharab
Dimag Kharab

Reputation: 4519

this should work,

**note thier is no need to use :eq selector here,

READ ABOUT JQUERY :eq selector

$('#lc_searchresult > table > tbody > tr').each(function() {
  var data = $(this).html();
  alert(data);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<div id='lc_searchresult'>
  <table>
    <tr>
      <td>654321</td>
      <td>13456</td>
    </tr>
  </table>
</div>

Upvotes: 0

James Martin-Davies
James Martin-Davies

Reputation: 651

Why eq it? You're using .each() which means you will return an array of <tr>'s.

$('#lc_searchresult > table > tbody > tr').each(function() {
    var data = $(this).find('td').text();
    alert(data);        
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="lc_searchresult">
    <table>
        <tbody>
            <tr>
                <td>654321</td>            
                <td>13456</td>        
            </tr>
            
            <tr>
                <td>4353535</td>            
                <td>3453553</td>        
            </tr>
        </tbody>
    </table>
</div>

JSFiddle.

Upvotes: 0

Related Questions