Javascript to alternate colours for table rows


<script language=”JavaScript” type=”text/JavaScript”>
function alternate(id){
   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”;
       rows[i].className = “odd”;

<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>




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>