I have an example of a typical JavaScript snippet (code that would apply a background style class to an alternating odd or even row in a table). I am trying to rewrite this in CoffeeScript, trying to find out. The CoffeeScript range syntax is different and more Ruby-esque. I would really appreciate an example of how you would do this?
function alternate(id){
if(document.getElementsByTagName){
var table = document.getElementById(id);
var rows = table.getElementsByTagName("tr");
for(i = 0; i < rows.length; i++){
if(i % 2 == 0){
rows[i].className = "even";
}else{
rows[i].className = "odd";
}
}
}
}
Refresh
I use jQuery and try to do this, but it does not work (it does all #efefef lines):
$(document).ready ->
rowCount = $('tbody tr')
for row in rowCount
if row.length % 2 == 0
$('tbody tr').css('background-color', '#363636')
else
$('tbody tr').css('background-color', '#efefef')
source
share