Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New Coder
    Join Date
    Mar 2012
    Posts
    60
    Thanks
    65
    Thanked 0 Times in 0 Posts

    count number of <td>s in first <tr> that has a <td>

    Hi

    How do I count the number of <td>s in first <tr> that has a <td>


    Code:
    $("table tr:has(td):first").length
    Code:
    <table>
    <tr>
    <th></th>
    <th></th>
    <th></th>
    </tr>
    <tr>
    <td></td>
    <td></td>
    <td></td>
    </tr>
    <tr>
    <td></td>
    <td></td>
    <td></td>
    </tr>
    <tr>
    <td></td>
    <td></td>
    <td></td>
    </tr>
    </table>

    thanks

  • #2
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,981
    Thanks
    56
    Thanked 557 Times in 554 Posts
    Code:
    $("table tr:has(td):first").children().size()

  • Users who have thanked xelawho for this post:

    code-in-time (06-13-2012)

  • #3
    New Coder
    Join Date
    Mar 2012
    Posts
    60
    Thanks
    65
    Thanked 0 Times in 0 Posts
    thanks

  • #4
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,981
    Thanks
    56
    Thanked 557 Times in 554 Posts
    or to be more specific
    Code:
    $("table tr:has(td):first > td").size()

  • #5
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,536
    Thanks
    45
    Thanked 259 Times in 256 Posts
    For the note, length and size will return the same data if you're trying to find out the number of elements selected, and length will be a bit faster in the long run. So its the ' > tr' or .children() that's really important.

  • #6
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,981
    Thanks
    56
    Thanked 557 Times in 554 Posts
    my reading of this test is that the native dom childElementCount is far better than either of the jQuery methods

    but some people prefer to keep their jQuery "pure" n which case $("table tr:has(td):first > td").size() performs better


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •