Set table alternative row background color using JavaScript


Example:

Heading
Row 1
Row 1
Row 2
Row 3
Row 4
Row 5
Row 6



Code Sample:

<html> <head> <title>Set table alternative row background color using JavaScript</title> </head> <body> <table align="center" id="tblMain" style="border: solid 1px black;" cellpadding="0" cellspacing="0" width="200px"> <th> Heading </th> <tr> <td> Row 1 </td> </tr> <tr> <td> Row 1 </td> </tr> <tr> <td> Row 2 </td> </tr> <tr> <td> Row 3 </td> </tr> <tr> <td> Row 4 </td> </tr> <tr> <td> Row 5 </td> </tr> <tr> <td> Row 6 </td> </tr> </table> <script language="javascript"> var tbl = document.getElementById("tblMain"); if (tbl != null) { if (tbl.rows[0] != null) { tbl.rows[0].style.backgroundColor = "#365890"; tbl.rows[0].style.color = "#FFFFFF"; } for (var i = 1; i < tbl.rows.length; i++) { if (i % 2 == 0) tbl.rows[i].style.backgroundColor = "#365890"; } } </script> </body> </html>