I’ve got an example here of a typical chunk of JavaScript (code that would apply a background style class to an alternating odd or even row in a table). I’m trying to rewrite this in CoffeeScript in my attempt to learn that. The CoffeeScript range syntax is different and more Ruby-esque. I’d 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++){
//manipulate rows
if(i % 2 == 0){
rows[i].className = "even";
}else{
rows[i].className = "odd";
}
}
}
}
Update
I’m using JQuery and trying this but it doesn’t work (it makes all rows #efefef):
$(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')
A little more concise: