Reputation: 20906
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
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
Reputation: 313
$('#lc_searchresult > table > tbody > tr').each(function() {
$(this).children('td').each(function(){
var data = $(this).html();
alert(data);
})
});
Upvotes: 2
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
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
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>
Upvotes: 0