JavaScript
  Home arrow JavaScript arrow Page 3 - 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 - Building zebra tables dynamically with a JavaScript function


    (Page 3 of 4 )

    Throughout the course of the previous section, I mentioned the possibility of using a pinch of JavaScript to dynamically generate the alternate background colors of even and odd rows contained in a specific table. This will achieve the characteristic zebra appearance without having to manually include the pertinent CSS classes into the markup.

    Based on this premise, I’m going to define a short JavaScript function that will be responsible for iterating over the even and odd rows of a determined (X)HTML table, and then alternate their respective background colors.

    Want to see how this brand new JavaScript function looks? Here is its simple signature:


    function buildZebraTable(tableId){

    var table=document.getElementById(tableId);

    if(!table){return};

    var evenFlag=false;

    var rows=document.getElementsByTagName('tr');

    for(var i=0;i<rows.length;i++){

    rows[i].className=!evenFlag?'oddrow':'evenrow';

    evenFlag=!evenFlag;

    }

    }


    As you can see, the logic implemented by the above “buildZebraTable()” JavaSCript function is quite simple to follow. Essentially, all that this function does is iterate over the rows of a specified (X)HTML table, and alternately style each of them via the assignment of two CSS classes, called “oddrow” and “evenrow” respectively. Obviously, the immediate result of this process, assuming that the pertinent CSS classes define different background colors, is the dynamic creation of a zebra table using only a short JavaScript function.

    Moreover, the previous “buildZebraTable()” function should be called after the target table has been loaded in the following way:


    window.onload=function(){

    if(document.getElementById&&document.
    getElementsByTagName&&document.createElement){

    buildZebraTable('zebratable');

    }

    }


    Building a zebra table with the help of JavaScript is a simple, yet effective process that only requires defining a function similar to the one that you learned earlier. However, once you've grasped the logic that stands behind this approach, it’d be more than desirable to put the mentioned JavaScript function, the CSS styles, and the markup of a sample (X)HTML table all together in one single file so you can see how they link with each other.

    Keeping this in mind, in the last section of this tutorial I’m going to build such a file, thus completing my demonstration of how to construct zebra tables through the proper combination of some basic CSS styles and a JavaScript function.

    To see the full definition of this (x)HTML file, please jump ahead and read the next few lines. We’re almost finished!

    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

    - Validating Digits and Dates with jQuery`s Va...
    - Validating Ranges, Emails, and URLs with jQu...
    - More Uses for the jQuery Tooltip Plug-in`s b...
    - Building Image-Based Tooltips with the jQuer...
    - Using the jQuery Tooltip Plug-in`s bodyHandl...
    - Using Rangelength, Min and Max with the Vali...
    - Using Minlength and Maxlength with the Valid...
    - Modifying Tooltip Coordinates with the jQuer...
    - Applying a Fade Out Effect with the jQuery T...
    - Tracking Mouse Movements with the jQuery Too...
    - Checking Online Forms with the Validator jQu...
    - Nested JavaScript Functions as Objects
    - The jQuery Tooltip Plug-in
    - Active Client Pages at the Server
    - ACP Tab Web Page







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
    Stay green...Green IT