jQuery to check the row count of a table


To get the row count of the table, in jQuery, all you need to do is use any of the following approaches:

$(“#mytable tr”).length

$(“#mytable tr”).size() //But the size method calls the length property internally

One thing to be noted here is that the indexing is 1-based and not 0-based. For ex.:

<html>
<head>
<script src=”jquery-1.3.2.min.js” type=”text/javascript” language=”javascript”></script>
<script type=”text/javascript” language=”javascript”>
$(document).ready(function(){
alert($(“#x tr”).length);
});

</script>
</head>
<body>
<table id=”x”>
<tr><td>1</td></tr>
<tr><td>2</td></tr>
<tr><td>3</td></tr>
<tr><td>4</td></tr>
<tr><td>5</td></tr>
<tr><td>6</td></tr>
<tr><td>7</td></tr>
<tr><td>8</td></tr>
</table>
</body>
</html>

Running the above html will give ‘8’ as the result and not 7.
//
//

6 thoughts on “jQuery to check the row count of a table

  1. I do not drop a comment, however after looking at through a few of the comments on this page
    jQuery to check the row count of a table | The Relentless FrontEnd – Aackose Lal’s Blog. I do have a few questions for you if it’s allright.
    Could it be simply me or does it look like a few of these comments appear like they
    are left by brain dead people? 😛 And, if
    you are writing at other online sites, I would like to follow you.
    Would you post a list of every one of your social sites like your Facebook page, twitter feed, or linkedin
    profile?

  2. Hi,
    Using $(“#mytable tr”).length didn’t work, at least in my environment.. but it worked using

    $(“#mytable tr”).children.length

    It could be because of the version of jquery?
    Cheers,
    Diego.

Feel free to leave a reply here...

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s