Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
10
rated 0 times [  13] [ 3]  / answers: 1 / hits: 76874  / 12 Years ago, tue, february 26, 2013, 12:00:00

I just want to count the number of rows,



   <button id=add>Add row</button>
<table>
<tbody id=mytbody>
</tbody>
</table>
Number of rows: <span id=counter></span>


Javascript:



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


I found this jQuery: count number of rows in a table



and this doesn't works http://jsfiddle.net/H8sBr/



I just don't get it working.
help?


More From » jquery

 Answers
35

The script is wrong, use append():



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


Demo: http://jsfiddle.net/praveenscience/H8sBr/115/


[#79987] Monday, February 25, 2013, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
destiniemartinac

Total Points: 92
Total Questions: 106
Total Answers: 111

Location: Cyprus
Member since Mon, Oct 24, 2022
2 Years ago
destiniemartinac questions
Thu, Oct 28, 21, 00:00, 3 Years ago
Sat, Mar 13, 21, 00:00, 3 Years ago
Wed, May 20, 20, 00:00, 4 Years ago
;