/ Published in: JavaScript
This function looks for tables in a web page, then loops through the rows and sets the backgroundColor for ever other row.This creates a striped table which is easier to read.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
function stripeTables() { if(!document.getElementsByTagName) return false;//basic old browser check var tables = document.getElementsByTagName("table");//grab the tables in the document for(i=0; i<table.length; i++) {//loop through the tables var odd = false;//creat an odd function for the table, set it to false var rows = tables[i].getElementsByTagName("tr");//grab the rows in the specific table for(j=0; j<rows.length; j++) {//loop through the rows in the specific table if(odd == true) { rows[j].style.backgroundColor = "#ffc";//if odd is true, set the bg color to this odd = false;//set odd to false } else { odd = true;//set odd to true, this is picked up by next iteration and styles the next row } } } }