stripeTables Funcion manual


/ Published in: JavaScript
Save to your folder(s)

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.


Copy this code and paste it in your HTML
  1. function stripeTables() {
  2. if (!document.getElementsByTagName) return false;
  3. var tables = document.getElementsByTagName("table");
  4. for (var i=0; i<tables.length; i++) {
  5. var odd = false;
  6. var rows = tables[i].getElementsByTagName("tr");
  7. for (var j=0; j<rows.length; j++) {
  8. if (odd == true) {
  9. addClass(rows[j],"odd");
  10. odd = false;
  11. } else {
  12. odd = true;
  13. }
  14. }
  15. }
  16. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.