1. Striped tables

  2. Breakdown

  3. Breakdown by method

  4. Code

    var tables = document.getElementsByTagName("table");
    for (var i=0; i<tables.length; i++) {
      var rows = tables[i].getElementsByTagName("tr");
      for (var j=0; j<rows.length; j=j+2) {
        rows[j].style.backgroundColor = "#ccc";
      }
    }
  5. A better breakdown

  6. Code

    var tables = document.getElementsByTagName("table");
    for (var i=0; i<tables.length; i++) {
      var rows = tables[i].getElementsByTagName("tr");
      for (var j=0; j<rows.length; j=j+2) {
        rows[j].className = "odd";
      }
    }
  7. Code with object detection

    if (document.getElementsByTagName) {
      var tables = document.getElementsByTagName("table");
      for (var i=0; i<tables.length; i++) {
        var rows = tables[i].getElementsByTagName("tr");
        for (var j=0; j<rows.length; j=j+2) {
          rows[j].className = "odd";
        }
      }
    }
  8. Finished example

    Create a striped effect for tables of data.

    View HTML