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>