3

How can I count the table rows that is added dynamically with jQuery?

I have tried with $('#mytbody').children().length; but it doesn't work with rows that are added dynamically.

Here is my code, also runnable in JsFiddle

<!DOCTYPE html>
<html>
<head>
<script src="jquery-1.6.2.min.js">
<script>
$(function() {
    $('#add').bind('click', function() {
        $('#mytbody').after('<tr><td>'+ new Date() +'</td></tr>');
        var count = $('#mytbody').children().length;
        $('#counter').html(count);
    });
});
</script>
</head>
<body>
<button id="add">Add row</button>
<table>
    <tbody id="mytbody">
    </tbody>
</table>
Number of rows: <span id="counter"></span>
</body>
</html>

5 Answers 5

4

http://jsfiddle.net/H8sBr/2/

you need to use .append() not .after(). After adds a element After your tbody but you count elements Inside your tbody. If you use append, you add them at the end of the tbody. Alternately you could use .prepend() to add entries on top of the table.

PS: This is a commun misconception because of the css selector .after() that adds content after the content of the selected element not after the element.

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

1 Comment

Hi @meo....thank a lot its working perfectly....I get solution for this with your help.
3

You are adding rows outside of tbody.

Change after to prepend

will work..

or change count to var count = $('table tr').length;

http://jsfiddle.net/H8sBr/442/

3 Comments

Thanks, changing after to prepend fixed my problem.
This jsfiddle doesn't work. The number of rows is always shown as zero. I don't understand how was it chosen as answer! Solution by @meo works great.
Hi @sankar.suda....Jsfiddle doesn`t work.Let me know how to get this ?How to solve this....
3

Try altering count to

var count = $('table tr').length;

This seems to work - not sure why acting on the tbody doesn't.

Edit: jsFiddle

Comments

1

Just use a counter,it has less dom lookups :

$(function() {
    $('#counter').html(0);
    var count = 1;
    $('#add').bind('click', function() {
        $('#mytbody').after('<tr><td>'+ new Date() +'</td></tr>');
        $('#counter').html(count);
        count++
    });
});

2 Comments

this would be perfect if you set the counter to the length of the tr that are already there... Maybe there could be some
This also works, but I prefer to not introduce another variable.
0

It looks there is some bug (not sure) in jQuery; row length is not getting updated if you add table row dynamically.

var rowCount = $("#tableId > tbody > tr").length; 

will not return correct count if you add row dynamically.

Simple solution is to use:

var rowCount = document.getElementById("tableId").rows.length;

Yes, you can use jQuery with javascript.

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.