1

I'm new with jquery and I want to count the existing elements from a table and the html structure is like:

<table summary="">
    <tr>
       <td>
           <table id="myid" some more attributes> <-- i got that id and 
               <tbody>
                   <tr>foo</tr> <---counting
                   <tr>bar</tr> <---counting
               </tbody>
            </table>
         </td>
      </tr>
 </table>
HTML is apex gernerated.

i've tried jquery statements from jQuery: count number of rows in a table an many more sits but nothing worked for me. My not working "solution" is

 $("table[id='myid'] > tbody >tr").length

or

 $('table#myid >tbody:last >tr').length

I'm thankful for any hints and tips

Mario

3
  • Including nested tables? Commented Aug 30, 2012 at 13:47
  • the id in your HTML is myid, but your jQuery selector is looking for table_id. Is this a typo on your part? Commented Aug 30, 2012 at 13:49
  • oh my bad. copy & past from my js file Commented Aug 30, 2012 at 13:57

7 Answers 7

3

Use $('table#myid tr').length​.

References:

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

1 Comment

However, that would also include rows from tables nested inside it, if there are any.
2

try this.

http://jsfiddle.net/L7Nea/1/

JS

$('#myid tbody tr').length); 

1 Comment

However, that would also include rows from tables nested inside it, if there are any.
1

What you have is what you should use, just with the right id:

$("table[id='myid'] > tbody > tr").length

or

$("table#myid > tbody > tr").length

Demo: http://jsfiddle.net/Guffa/bbV3Z/

As the id should be unique in the page, you should be able to use just:

$("#myid > tbody > tr").length

The reason for using #myid > tbody > tr instead of just #myid tr is to make sure that you only get the rows in that specific table, and not rows from tables nested inside it. If you don't have any tables nested inside it, you can just use:

$("#myid tr").length

Comments

1

if you mean inside your specific table

$('#myid tr').length // <-- this gets all tr inside table with id=myid

Or if you really wanted to traverse the dom and be more specific in case theres another table inside that table

$('#myid > tbody > tr').length

Plus you're already using the ID selector so no need to have table#myid

1 Comment

That will count the row in the outer table also.
0

You can use $("#table_id tr").length

1 Comment

However, that would also include rows from tables nested inside it, if there are any.
0

Use var count = $('#myid tr').length;.

1 Comment

However, that would also include rows from tables nested inside it, if there are any.
0

Think about performances also by takeing the context first :

$('#myid').find('tr')

here's the perfs comparisons : http://jsperf.com/tr-number

1 Comment

I've also added all other answer snippets on this thread for comparison, feel free to look at it...

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.