benjamin.morgan
New Member
I know, I could use jquery but this isn't about that. I want to be able to alternate a table's row colors, but it isn't working. I have the code in a jsfiddle so you can run it (or edit it). Any help appreciated and letting me know where it went wrong is appreciated too.
http://jsfiddle.net/Ws34g/
http://jsfiddle.net/Ws34g/
HTML:
<!DOCTYPE HTML>
<html lang="en">
<head>
<style>
.odd{background-color: white;}
.even{background-color: gray;}
</style>
</head>
<body>
<table>
<tr><td>0 - some txt</td></tr>
<tr><td>1 - some txt</td></tr>
<tr><td>2 - some txt</td></tr>
<tr><td>3 - some txt</td></tr>
<tr><td>4 - some txt</td></tr>
</table>
<script>
* *var rows = getElementsByTagName("tr");
* *for(i = 0; i < rows.length; i++){ * * * * *
* * *if(i % 2 == 0){
* * * *rows[i].className = "even";
* * *}else{ <br>
* * * *rows[i].className = "odd";
* * *} * * *
}
</script>
</body>
</html>