Reputation: 5880
I have been trying to count the total number of tr's located in my nested tbody and I am unable to get the total count. I have tried to get it using the following jQuery code but its returning a big number like 44 rows, where as I am expecting it to return 7 rows.
Could you suggest me as to where I am going wrong?
var count = $("#rbe_viewTab0 table tbody tr td table tbody tr").length;
alert(count);
<div id="rbe_viewTab0" class="nothing" style="display:block;margin-top:0px" isloaded="Y">
<table class="wide" cellspacing="0" cellpadding="10" border="0">
<tbody>
<tr>
<td>
<table class="wide" cellspacing="0" cellpadding="10" border="0">
<tbody>
<tr></tr>
<tr>
<td id="xyz">Row 2</td>
</tr>
<tr></tr>
<tr></tr>
<tr></tr>
<tr></tr>
<tr></tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
</div>
I am expecting it to return 7 rows for the total number of rows and index as '1' for Row 2.
Upvotes: 1
Views: 5429
Reputation: 5683
I think your selector may be wrong. Here is a snippet.
var count = $("#rbe_viewTab0 > tbody > tr > td > table.wide > tbody > tr").length;
var index = $("#rbe_viewTab0 > tbody > tr > td > table.wide > tbody > tr > td#xyz").parent().index();
alert(count);
alert(index);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<table id="rbe_viewTab0">
<tr>
<td>
<table class="wide">
<tr>
<td>Row 1</td>
</tr>
<tr>
<td id="xyz">Row 2</td>
</tr>
<tr>
<td>Row 3</td>
</tr>
<tr>
<td>Row 4</td>
</tr>
<tr>
<td>Row 5</td>
</tr>
<tr>
<td>Row 6</td>
</tr>
</table>
</td>
</tr>
</table>
EDIT Here is the selector for the index. Also updated my snippet.
var index = $("#rbe_viewTab0 > tbody > tr > td > table.wide > tbody > tr > td#xyz").parent().index();
Upvotes: 2
Reputation: 689
$('tbody tbody tr').length
would do.
e.g. http://jsbin.com/bacosijina/1/edit?html,output
Edit: Now, that you've updated the question:
$('table table tr').length
would do. i.e. rows in a table contained by a table
Edit Edit: Ok, the markup in the question changed yet again. Maybe you want to go with $('#rbe_viewTab0 tbody tbody tr').length
e.g. http://jsbin.com/suxolo/1/edit?html,output
Upvotes: 1
Reputation: 4230
Try
var count = $("#rbe_viewTab0 > table > tbody > tr > td > table > tbody > tr").length;
alert(count);
Upvotes: 1