tags:

views:

1400

answers:

7

I have seen several examples on how to find children, etc but for some reason I can't get this one right ... any help from the SO community?

The below is my current "attempt" in code --

var trCount = $("#table > tr").size();

+7  A: 

This should work:

var count = $("#table tr").length;
David Brown
this is exactly what I was looking for!
Toran Billups
A: 

It should work, but I believe what you want is:

var trCount = $("table > tr").size();

If you use #table, you're asking for a table with id='table' which I guess is an error?

alphadogg
+1  A: 

Without the descendant selector works for me, but not with.
works:

var trCount = $("table tr").size();

doesnt:

var trCount = $("table > tr").size();

Also make sure you put the declaration inside the docready or it wont work.

$(document).ready(function() {
     var trCount = $("table tr").size();
});
garrow
+1  A: 

Try:

var trCount = $("table tr").size();

Using > may not work if you use a tbody, thead etc.

Also, the # will select an element, any element, with an id of table. Just be sure this is what you want.

Ash
+1  A: 

Browsers automatically add a tbody element to the DOM.

var trCount = $("#table > tbody > tr").size();

The "table tr" answers posted will not work correctly with nested tables.

Miles
+2  A: 

Plain Old DOM:

document.getElementById('table').rows.length;

is going to be much more efficient than asking jQuery to go work out the selector and return every row element for you.

You don't have to force everything you do into a jQuery-shaped hole; sometimes the old ways are still the best. jQuery was created to supplement JavaScript in the areas it was weak, not completely replace the entire language.

$("#table tr").size();

Fails for nested tables.

$("#table > tr").size();

Fails for tr in tbody (which is very often the case as they can get inserted automatically).

$("#table > tbody > tr").size();

Fails for tr in thead/tfoot.

bobince
A: 

What if I have more than one table.?

Deepak
So you want to find the number of TR elements for each table element in the DOM?
Toran Billups