dan_vitch dan_vitch - 4 months ago 17
Javascript Question

Getting more than expected with siblings().length

I have the following html

<div id="myDiv">
<table>
<tbody>
<tr></tr>
<tr></tr>
<tr></tr>
<tr></tr>
<tr></tr>
<tr></tr>
<tr></tr>
<tr></tr>
</tbody>
</table>
</div>


When I execute this jquery code

var count = $('#myDiv table tbody tr').siblings().length;


I am expecting to get 8 as the value of count, instead I am getting 45. There is more html on the page than the area I have shown, and each of the above elements hold various amounts of data. What am I missing?

Answer
var count = $('#myDiv table tbody tr').length;

or

var count = $('#myDiv tr').length;