We Recommend

Pro JavaScript Techniques Pro JavaScript Techniques
Pro JavaScript Techniques is the ultimate JavaScript book for the modern web developer. It provides everything you need to know about modern JavaScript, and shows what JavaScript can do for your web sites. This book doesn't waste any time looking at things you already know, like basic syntax and structures.


Posted By

1man on 02/13/07


Tagged

table style function stripes


Versions (?)


Who likes this?

2 people have marked this snippet as a favorite

shachi
vali29


Stripe Table Rows DOM JS Function


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.

  1. function stripeTables() {
  2. if(!document.getElementsByTagName) return false;//basic old browser check
  3. var tables = document.getElementsByTagName("table");//grab the tables in the document
  4. for(i=0; i<table.length; i++) {//loop through the tables
  5. var odd = false;//creat an odd function for the table, set it to false
  6. var rows = tables[i].getElementsByTagName("tr");//grab the rows in the specific table
  7. for(j=0; j<rows.length; j++) {//loop through the rows in the specific table
  8. if(odd == true) {
  9. rows[j].style.backgroundColor = "#ffc";//if odd is true, set the bg color to this
  10. odd = false;//set odd to false
  11. } else {
  12. odd = true;//set odd to true, this is picked up by next iteration and styles the next row
  13. }
  14. }
  15. }
  16. }

Report this snippet 

You need to login to post a comment.