DelphiFAQ Home Search:

Obtain the number of rows in a table

 

comments11 comments. Current rating: 5 stars (6 votes). Leave comments and/ or rate it.

Question:

How can I obtain the number of rows in an HTML table using javascript?

Answer:

The following code assumes that your table has the ID 'MyTable'

<script language="JavaScript">
<!--
var oRows = document.getElementById('MyTable').getElementsByTagName('tr');
var iRowCount = oRows.length;

alert('Your table has ' + iRowCount + ' rows.');
//-->
</script>

Comments:

2007-03-19, 14:22:13
anonymous from United States  
It is great, Thank u
2007-08-24, 08:06:32
anonymous from United Kingdom  
wicked, cheers
2007-11-01, 01:11:03
anonymous from Sweden  
rating
Simple and perfect for me today. thankyou.
2007-11-01, 09:48:23
anonymous from United States  
rating
thank you
2007-11-28, 15:36:55
anonymous from Australia  
rating
Just what I needed. Thanks very much.
2008-01-22, 10:00:54
anonymous from Mairiporã, Brazil  
rating
Very simple and helful! Thanks!

In one row:

alert(document.getElementById('MyTable').getElementsByTagName('tr').length);
2008-03-04, 11:19:00
anonymous from Germany  
Thanks, useful
2008-05-02, 22:12:10
anonymous from India  
Thanks for your great help :)
2008-08-28, 09:41:29
anonymous from United States  
rating
Great! Thanks. Sure beats using a SQL count...
2008-09-17, 03:22:27   (updated: 2008-09-17, 03:24:44)
anonymous from Germany  
If a html table has inner tables then the above logic does not work. Could please show to get the no of rows for outer table ? Same is true for column.
2008-09-24, 00:42:37
Akif from India  
rating
Thnx Yaar

 

 

Email address (not necessary):

Rate as
Hide my email when showing my comment.
Please notify me once a day about new comments on this topic.
Please provide a valid email address if you select this option.
 
It seems that you are
from Los Angeles, US .

Info/ Feedback on this

Show city and country
Show country only
Hide my location
You can mark text as 'quoted' by putting [quote] .. [/quote] around it.
Please type in the code:
photo Add a picture:

Please do not post inappropriate pictures. Inappropriate pictures include pictures of minors and nudity. The owner of this web site reserves the right to delete such material.