JavaScript
  Home arrow JavaScript arrow 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 
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

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 / 2
    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


    (Page 1 of 4 )

    Welcome to the second installment of the series “Building Zebra Tables with CSS and JavaScript.” Comprised of four approachable tutorials, this series shows you different methodologies to help you create the so-called zebra tables with CSS. In addition, it teaches you how to use JavaScript to dynamically generate the alternate background colors of their respective even and odd rows.

    As its name suggests, a zebra table is simply a regular (X)HTML table that uses one background color for its even rows and a different color for its odd ones. This makes it extremely useful for providing tabular data with a true professional appearance. However, despite the fact that these tables have become very popular with many modern web designers, admittedly, they’re pretty hard to code from scratch, especially if they’re not generated by a server-side scripting language.

    Nonetheless, it’s possible, via the assistance of JavaScript, to dynamically assign alternated background colors to the table’s even and odd rows once the table in question has been created with standard markup. This powerful technique is the main focus of this series of articles. Thus, if you want to learn how to put it to work for you in a short time, then jump ahead and start reading right now!

    Having already introduced the main subject of this article series, I think this is an appropriate moment to rehash the group of topics that were covered in the last article of the series, with reference to building basic zebra tables using a simple combination of CSS styles and structural markup.

    Speaking specifically about this process, I’m sure that with all of the code samples that you learned in the mentioned tutorial, you’re armed with the required background to start incorporating a few zebra tables into your own web sites, especially if you want to provide your tabular data with a polished and professional look and feel.

    As you saw in the previous article, a zebra table can be easily constructed by defining two (or eventually only one) CSS classes, which are assigned alternately to the odd and even rows of the table in question. However, the major drawback to building it in this manner is that each of the CSS classes has to be included manually into the markup via a “class” attribute.

    Actually, this process can be quite annoying and time-consuming, particularly if the table being created contains many rows. Thus, in the course of this second installment of the series, I’m going to show you how to use JavaScript to assign background colors of the pertinent even and odd rows of the table, thus, completely automating this process.

    So, are you ready to delve deeper and learn how to build zebra tables with JavaScript? Let’s get started!

    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 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 5 hosted by Hostway
    Stay green...Green IT