JavaScript
  Home arrow JavaScript arrow Page 2 - Assigning Background Colors Dynamically to...
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVASCRIPT

Assigning Background Colors Dynamically to Zebra Tables with CSS and JavaScript
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 3
    2008-06-18

    Table of Contents:
  • Assigning Background Colors Dynamically to Zebra Tables with CSS and JavaScript
  • Review: building a basic zebra table using a CSS-based approach
  • Building zebra tables dynamically with a JavaScript function
  • Setting up a final hands-on example

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Assigning Background Colors Dynamically to Zebra Tables with CSS and JavaScript - Review: building a basic zebra table using a CSS-based approach


    (Page 2 of 4 )

    Before I proceed to show you how to use the assistance of JavaScript to build zebra tables dynamically, first I’d like to reintroduce a concrete hands-on example that was developed in the prior article of the series. Its primary objective was to illustrate how to create this type of table via a conventional CSS-based approach.

    Having said that, please take a quick look at the following code sample, which constructs a primitive zebra table using two simple classes:


    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml">

    <head>

    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

    <title>Example on building a simple zebra table with CSS (both odd and even rows are styled)</title>

    <style type="text/css">

    body{

    padding: 0;

    margin: 0;

    background: #fff;

    }

    h1{

    font: bold 16pt Arial, Helvetica, sans-serif;

    color: #000;

    }

    p{

    font: normal 10pt Arial, Helvetica, sans-serif;

    color: #000;

    margin: 0;

    }

    #zebratable{

    width: 40%;

    text-align: center;

    }

    #zebratable tbody tr.evenrow td{

    background: #eee;

    padding: 10px;

    }

    #zebratable tbody tr.oddrow td{

    background: #ccc;

    padding: 10px;

    }

    </style>

    </head>

    <body>

    <h1>Example on building a simple zebra table with CSS</h1>

    <table id="zebratable">

    <tbody>

    <tr class="oddrow">

    <td><p>Content for cells of odd row goes here</p></td>

    </tr>

    <tr class="evenrow">

    <td><p>Content for cells of even row goes here</p></td>

    </tr>

    <tr class="oddrow">

    <td><p>Content for cells of odd row goes here</p></td>

    </tr>

    <tr class="evenrow">

    <td><p>Content for cells of even row goes here</p></td>

    </tr>

    <tr class="oddrow">

    <td><p>Content for cells of odd row goes here</p></td>

    </tr>

    <tr class="evenrow">

    <td><p>Content for cells of even row goes here</p></td>

    </tr>

    <tr class="oddrow">

    <td><p>Content for cells of odd row goes here</p></td>

    </tr>

    <tr class="evenrow">

    <td><p>Content for cells of even row goes here</p></td>

    </tr>

    <tr class="oddrow">

    <td><p>Content for cells of odd row goes here</p></td>

    </tr>

    <tr class="evenrow">

    <td><p>Content for cells of even row goes here</p></td>

    </tr>

    </tbody>

    </table>

    </body>

    </html>


    After examining the above hands-on example in detail, you’ll have to agree with me that building a rudimentary zebra table by using a couple of CSS classes is a very straightforward process that can be performed with minor hassles.

    For this specific case, “the zebra effect” (doesn’t it sound like a movie title?) is achieved by assigning a different CSS class to even and odd rows of the table, which you’ve probably done hundreds of times before. Nonetheless, as I said before, including those CSS classes manually into the table’s markup can be hard work, especially if the table is large and contains numerous rows.

    Nonetheless, it’s perfectly possible to address the aforementioned issue with the help of JavaScript, since it can be used to dynamically assign the respective CSS classes to the table’s rows, thus, automating this rather annoying task.

    This JavaScript-based approach can be quite helpful when it comes to building zebra tables, so if you’re interested in learning how to put it to work for you, then simply read the following section.

    More JavaScript Articles
    More By Alejandro Gervasio


       · The first chapter of the series went through building zebra tables with CSS. Though,...
       · replacing var rows = document.getElementsByTagName('tr');with var...
       · Thank you for the comments on my article. Yes, the script can be slightly improved...
     

    JAVASCRIPT ARTICLES

    - Using jQuery to Preload Images with CSS and ...
    - Using Client-Side Scripting to Preload Image...
    - Removing Non-Semantic Markup when Preloading...
    - Using the Display CSS Property to Preload Im...
    - Preloading Images with CSS and JavaScript
    - Scaling and Moving Web Page Elements with th...
    - Fading, Hiding and Sliding HTML Elements wit...
    - Toggling CSS Properties with the GX JavaScri...
    - Cancel, Queue and Pause Animations with the ...
    - Producing Elastic Effects with the GX JavaSc...
    - Moving Divs Diagonally with the GX JavaScrip...
    - Moving Elements Vertically and Horizontally ...
    - Making Bouncing Effects in Parallel with the...
    - Creating Bouncing Effects with the GX JavaSc...
    - Manipulating Background Colors with the GX J...







    © 2003-2010 by Developer Shed. All rights reserved. DS Cluster 4 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek