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();
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();
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?
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();
});
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.
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.
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.