JavaScript
  Home arrow JavaScript arrow Page 3 - Using the Style Object for Zebra Tables wi...
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 
Sun Developer Network 
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

Using the Style Object for Zebra Tables with CSS and JavaScript
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 1
    2008-07-02

    Table of Contents:
  • Using the Style Object for Zebra Tables with CSS and JavaScript
  • Building zebra tables with JavaScript
  • Building zebra tables with the style JavaScript object
  • Using the previous JavaScript function to build a zebra table

  • 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


    Using the Style Object for Zebra Tables with CSS and JavaScript - Building zebra tables with the style JavaScript object


    (Page 3 of 4 )

    As you might have guessed, using the “style” JavaScript object to alternately assign the background colors of even and odd rows of a given (X)HTML table is actually an easy process that can be accomplished with minor hassles.

    Essentially, implementing this approach requires introducing only a few changes to the “buildZebraTable()” JavaScript function that you learned before, but the logic that drives this function will remain nearly the same.

    So, in order to demonstrate this concept, I listed a modified version of the aforementioned “buildZebraTable()” function that uses the “style” JavaScript object to decorate the rows of a targeted table. Here’s how this function now looks:


    function buildZebraTable(tableId){

    var table=document.getElementById(tableId);

    if(!table){return};

    // get all <tbody> table elements

    var tbodies=document.getElementsByTagName('tbody');

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

    var evenFlag=false;

    // get all <tr> table elements

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

    for(var j=0;j<trows.length;j++){

    // assign background color to even and odd rows

    trows[j].style.backgroundColor=!evenFlag?'#eeeeee':'#cccccc';

    evenFlag=!evenFlag;

    }

    }

    }


    If you examine the modified signature of the above “buildZebraTable()” JavaScript function, you’ll have to agree with me that it’s extremely easy to grasp. As you can see, the function in question now uses a “style” object, instead of a CSS class, to assign different background colors to the rows of the targeted table, thus, achieving its “zebra” appearance.

    Obviously, the rest of the function remains practically the same, so you shouldn’t have major problems understanding how it works. It’s fair to mention here that there are certain aspects of the function that might be improved. For instance, it could accept the background colors that will be applied to the even and odd rows of the table as input arguments, but this modification will be left as homework for you.

    So far, so good. At this point you hopefully know how to build a JavaScript function that utilizes the non-standard “style” object to create simple zebra tables. But you'll most likely want to see how this function can be used in the context of a concrete example.

    Thus, in the last section of this tutorial I’ll set up a hands-on example aimed at demonstrating the capacity of the recently-defined JavaScript function when it comes to constructing zebra tables dynamically.

    Go ahead and read the next few lines. They’re only one click away!

    More JavaScript Articles
    More By Alejandro Gervasio


       · In this final chapter of the series, you’ll learn how to build zebra tables by using...
       · Not every table has an element tbody.
       · Thank you for commenting on my article. Precisely, this condition was taken into...
     

    JAVASCRIPT ARTICLES

    - Using Click Interceptions with a Database-Dr...
    - Using JavaScript Click Interceptions in an I...
    - Using Click Interceptions with JavaScript
    - QuickSort in Action
    - Quicksort
    - Using Mod_Security to Protect Your Server
    - Detecting and Countering Server Intrusions
    - Securing Your Web Server
    - Building a Secure Web Server
    - Protecting the Server
    - Book Review: Learning the Yahoo! User Interf...
    - Dynamically Generate a Selection List in a R...
    - Intergrate DWR into Your Java Web Application
    - Detect Browser Compatibility with the Reques...
    - Using the EXT JS Date Picker Widget






    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
    Stay green...Green IT