Return to Snippet

Revision: 2423
at February 13, 2007 06:29 by 1man


Initial Code
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
			}
		}
	}
}

Initial URL

                                

Initial Description
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.

Initial Title
Stripe Table Rows DOM JS Function

Initial Tags
table, style, function

Initial Language
JavaScript