Reputation: 110940
w jQuery - is it possible to setup a listener that can count the number of rows in a table.
And when the row count changes do X if the count is eq 0 or Y is > 0?
Thanks
Upvotes: 1
Views: 2928
Reputation: 253308
You could use something like:
$('#tableID').bind('DOMNodeInserted', function() {
var count = $('table tr').length;
alert("The number of rows has changed, there are now " + count + " rows.");
});
Upvotes: 0
Reputation: 1160
One way is to use time listener :
var time = setInterval(function(){
alert( $('#table tr').length );
},1000 );
Or you can put it when the event associated with change num rows executed .
Upvotes: 1
Reputation: 107686
Both .length and .size() will give you the same information. If the table ID is tbl1
, then you use the jQuery CSS selector
$('#tbl1 tr')
If you are using <thead>
, <tbody>
, <tfoot>
properly, then you should use the below to count only the body rows, otherwise take 1 or 2 off the .length
result from the above.
$('#tbl1 tbody tr')
As for observing the row count changes, you need either
Upvotes: 0