I have the following HTML structure and I wanted to find out the length of immediate
s. here is the code that I am using:- Solution 1:
I removed the asterisks out of your HTML and made some assumptions about how you're invoking getBody
, so if I did anything that wasn't right, let me know.
Code: http://jsfiddle.net/27ygP/
function getBody (element ) {
var divider = 2 ;
var originalTable = element.clone ();
var tds = $(originalTable).children ('tbody' ).children ('tr' ).children ('td' ).length ;
alert (tds);
}
getBody ($('table.PrintTable' ));
Copy The big change was the add a .children('tbody')
. The HTML interpreter wraps the tr
s in tbody
. Traverse down into that, and you'll be fine.
Solution 2:
I think you want to use the following.
$("td" ).length
Copy UPDATE
You will want to use the tr tag as the start selector and then count each td selector using first to take just the first one.
$("tr" , $("td:first" )).length
Copy Solution 3:
Try this:
$(document ).ready (function ( ){
var countTD=$("Your_Table_ID_or_Class tr:first > td" ).length ;
});
$(window ).load (function ( ){
var countTD=$("Your_Table_ID_or_Class tr:first > td" ).length ;
});
Copy Note; If you creating dynamic table row column then use
$(document).live("click",function(){});
Solution 4:
This should work:
var itemsCount = $(".PrintTable > tr > td" ).length ;
Copy Update:
I just realized that at least Chrome inserts <tbody>
if it isn't already present, so to get cross browser support:
var itemsCount = $(".PrintTable > tbody > tr > td, .PrintTable > tr > td" ).length ;
Copy Example: http://jsfiddle.net/H2JWS/
Solution 5:
In general, if you want to count the number of td
's in a specific row, you can do this..
$(function ( ){
count = $("table tr" ).children ("td" ).index ()+1 ;
});
Copy
Post a Comment for "Finding The Count Of Jquery"