Javascript to alternate colours for table rows

<html>
<head>

<script language=”JavaScript” type=”text/JavaScript”>
function alternate(id){
 if(document.getElementsByTagName){ 
   var table = document.getElementById(id);  
   var rows = table.getElementsByTagName(“tr”);  
   for(i = 1; i < rows.length; i++){          
 //manipulate rows
     if(i % 2 == 0){
       rows[i].className = “even”;
     }else{
       rows[i].className = “odd”;
     }      
   }
 }
}
</script>
</head>

<body onload=”alternate(‘thetable’)”>
<table width=”100%” id=”thetable”>

<tr><td>first colour</td></tr>

<tr><td>second colour</td></tr>

<tr><td>first colour</td></tr>

<tr><td>second colour</td></tr>

</table>

</body>

</html>

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>