Reputation: 1
I am using the following script to add a unique class to each row of a table. Works nicely. However if I have more than one table there's a problem. If the first table has 50 rows then the first row of the second table starts at 51. Not what I am after. Any ideas?
$(document).ready(function() {
$('table').each(function() {
$('table tbody > tr').each(function() {
var rcount = 1;
$("tr").attr("class", function() {
return "row" + rcount++;
});
});
});
});
Upvotes: 0
Views: 785
Reputation: 1
Did it this way in the end:
$(document).ready(function() {
$('table').each(function() {
var rcount = 1;
$(this).find('tr').each(function() {
$(this).attr("class", function() {
return "row" + rcount++;
});
});
});
});
Upvotes: 0
Reputation: 3338
Replace $('table tbody > tr')
with $(this).children('tbody > tr')
and $("tr")
with $(this)
:
$(document).ready(function() {
$('table').each(function() {
$(this).children('tbody > tr').each(function() {
var rcount = 1;
$(this).attr("class", function() {
return "row" + rcount++;
});
});
});
});
Upvotes: 2