12

I have the following HTML structure and I wanted to find out the length of immediate <td>s. here is the code that I am using:-

<table class="PrintTable">
    <tr>
      **<td>**
        <table>
            <thead>
                <tr><th>Type Of Transaction</th></tr>
            </thead>
            <tbody>
                <tr>
                    <td>Name</td>
                </tr>
                <tr>
                    <td>Age</td>
                </tr>
            </tbody>
        </table>
      </td>
      **<td>**
        <table>
            <thead>
                <tr><th>2006</th></tr>
            </thead>
            <tbody>
                <tr>
                    <td>Andi</td>
                </tr>
                <tr>
                    <td>25</td>
                </tr>
            </tbody>
        </table>
      </td>

    </tr>
</table>

The function that I am using to find out the length of td is

function getBody(element)
{
    var divider=2;
    var originalTable=element.clone();
    var tds = $(originalTable).children('tr').children('td').length;
    alert(tds);


}

The result I am seeing is 0. No clue at all. I am expecting 2. Any help will be appreciated.

2
  • Why are you using .clone()? And is element meant to be a jQuery object? I assume so, since clone is not a native JS method on a DOM object. Can you please provide an example of how you're invoking getBody? Commented Jan 18, 2011 at 21:00
  • Yes, element is a JQuery Object and it contains the complete table as object. Even if you do not invoke clone(), still it shows 0 where as I am expecting 2. Commented Jan 18, 2011 at 21:05

7 Answers 7

24

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'));

The big change was the add a .children('tbody'). The HTML interpreter wraps the trs in tbody. Traverse down into that, and you'll be fine.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks a lot. It works. Is there a good debugger where we can find out all these tricks.
Firebug for Firefox is the best tool on the market to navigate the DOM. All of the major browsers have built-in tools to do the same (just click around the menus). It will take you to, among other things, an interactive tree that you can traverse manually.
Thanks! I wonder why you cant use multiple selectors with .children()
5

I think you want to use the following.

$("td").length

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

1 Comment

Yes, but not for the nested ones but only for the <td>.
5

Try this:

//For FFox
$(document).ready(function(){
var countTD=$("Your_Table_ID_or_Class tr:first > td").length;
});

// For webKit Browser
$(window).load(function(){
var countTD=$("Your_Table_ID_or_Class tr:first > td").length;
});

Note; If you creating dynamic table row column then use $(document).live("click",function(){});

Comments

1

This should work:

var itemsCount = $(".PrintTable > tr > td").length;

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;

Example: http://jsfiddle.net/H2JWS/

Comments

1

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; 
});

Comments

1

var rowCount = $('#myTable tr').length;

Comments

0
var originalTable = $(remove_copy_drargelement).closest('table').clone();

var tds = $(originalTable).children('tbody').children('tr').children('td').length;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.