Return to Snippet

Revision: 50422
at August 20, 2011 20:41 by alberomo


Initial Code
function stripeTables() {
  if (!document.getElementsByTagName) return false;
  var tables = document.getElementsByTagName("table");
  for (var i=0; i<tables.length; i++) {
    var odd = false;
    var rows = tables[i].getElementsByTagName("tr");
    for (var j=0; j<rows.length; j++) {
      if (odd == true) {
        addClass(rows[j],"odd");
        odd = false;
      } else {
        odd = true;
      }
    }
  }
}

Initial URL


Initial Description
A JS function that stripes the tables ("table" elements) of a document. It takes no arguments. Add a new class ("odd") to the odd rows that needs to be styled using CSS. Use addClass function.

From book Dom Scripting by Jeremy Keith.

Initial Title
stripeTables Funcion manual

Initial Tags
javascript

Initial Language
JavaScript