spacer
home / programming / javascript / jf / column7 / 1 current pageTo page 2To page 3
[next]

Senior Consultant/Information Security - permanent position (TX)
Next Step Systems
US-TX-Irving

Justtechjobs.com Post A Job | Post A Resume
Developer News
Mandrake Linux Founder Back, Virtually
Amazon: We're a Technology Company
Sun Expands MySQL With Closed Source

Easy Table Reading in JavaScript

Readability is a key factor on any web site as it enhances the user experience. If a user suffers from a partial visual impairment it can help them pinpoint information with greater ease. This article focuses on taking advantage of JavaScript's style-alternating capabilities and event-handlers to make any dynamic web page easier to read in a few seconds.

Let's begin with a sample page:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Strict//EN"
  "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en" dir="ltr">
  <head>
    <title>Example</title>
    <meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
    <meta http-equiv="content-script-type" content="text/javascript">
    <meta http-equiv="content-style-type" content="text/css">
    <style type="text/css"><!--
    .tbl {
       width: 100%;
       border: 1px solid #000;
       border-collapse: collapse;
       font-family: arial, sans-serif;
       font-size: small;
    }
    .tbl td, .tbl th {
       text-align: center;
       border: 1px solid #000;
    }
    .tbl th {
       background-color: #aaf;
    }
    .tbl .on {
       background-color: #ddd;
    }
    .tbl .off {
       background-color: #fff;
    }
    .tbl .hover {
       background-color: #99f;
    }
    .tbl .click {
       background-color: #66f;
       font-weight: bold;
    }
    --></style>
</head>
  <body>
    <table class="tbl">
      <tr><th>Word Word</th><th>Word Word</th><th>Word Word</th></tr>
      <tr><td>word word</td><td>word word</td><td>word word</td></tr>
      <tr><td>word word</td><td>word word</td><td>word word</td></tr>
      <tr><td>word word</td><td>word word</td><td>word word</td></tr>
      <tr><td>word word</td><td>word word</td><td>word word</td></tr>
      <tr><td>word word</td><td>word word</td><td>word word</td></tr>
      <tr><td>word word</td><td>word word</td><td>word word</td></tr>
      <tr><td>word word</td><td>word word</td><td>word word</td></tr>
      <tr><td>word word</td><td>word word</td><td>word word</td></tr>
      <tr><td>word word</td><td>word word</td><td>word word</td></tr>
      <tr><td>word word</td><td>word word</td><td>word word</td></tr>
      <tr><td>word word</td><td>word word</td><td>word word</td></tr>
      <tr><td>word word</td><td>word word</td><td>word word</td></tr>
      <tr><td>word word</td><td>word word</td><td>word word</td></tr>
      <tr><td>word word</td><td>word word</td><td>word word</td></tr>
      <tr><td>word word</td><td>word word</td><td>word word</td></tr>
      <tr><td>word word</td><td>word word</td><td>word word</td></tr>
      <tr><td>word word</td><td>word word</td><td>word word</td></tr>
      <tr><td>word word</td><td>word word</td><td>word word</td></tr>
      <tr><td>word word</td><td>word word</td><td>word word</td></tr>
      <tr><td>word word</td><td>word word</td><td>word word</td></tr>
    </table>
  </body>
</html>

In the code above we have a simple HTML document with twenty rows and three columns. We have applied Cascading Stylesheets (CSS) to the table to format it. Notice the class="tbl" part of the code - this is the class to format the table. By setting a class on specific tables, we can tell JavaScript only to target specific tables. This way, if you don't want a table to be automatically formatted for any particular reason, you can avoid it by simply not adding the class="tbl" to the table tag. You'll notice other assorted CSS tags in the document as well, these are sub-classes that we will use in our JavaScript to change the background colors and other styling information - at will.

Let's move on to the first part of our script - alternating colors. We want to take one row, color its background one color (using the "on" class), take the next row, and color its background another color (using the "off" class). Adding the following script inside the HEAD tags will do just that:

      <script type="text/javascript"><!--
        var elem = "TR";

window.onload = function(){
  if(document.getElementsByTagName){
    var el = document.getElementsByTagName(elem);
      for(var i=0; i<el.length; i++){
          if(el[i].childNodes[0].tagName != "TH"
          && el[i].parentNode.parentNode.className.indexOf("tbl") != -1){
        if(i%2 == 1){
          el[i].className = "on";
      } else {
         el[i].className = "off";
     }
    }
  }
}
  //--></script>

home / programming / javascript / jf / column7 / 1 current pageTo page 2To page 3
[next]

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info

Legal Notices, Licensing, Reprints, Permissions, Privacy Policy.
Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

Whitepapers and eBooks

Symantec Whitepaper: Converging System and Data Protection for Complete Disaster Recovery
Intel Whitepaper: Comparing Two- and Four-Socket Platforms for Server Virtualization
IBM Solutions Brief: Go Green With IBM System xTM And Intel
HP eBook: Simplifying SQL Server Management
IBM Contest: Are You the Next Superstar? Join the "Search for the XML Superstar" Contest to Find Out
Microsoft PDF: Top 10 Reasons to Move to Server Virtualization with Hyper-V
Microsoft PDF: Six Reasons Why Microsoft's Hyper-V Will Overtake Vmware
Microsoft Step-by-Step Guide: Hyper-V and Failover Clustering
Intel PDF: Quad-Core Impacts More Than the Data Center
Intel PDF: Virtualization Delivers Data Center Efficiency
Go Parallel Article: PDC 2008 in Review
Microsoft PDF: Top 11 Reasons to Upgrade to Windows Server 2008
Avaya Article: Communication-Enabled Mashups: Empowering Both Business Owners and IT
Intel Whitepaper: Building a Real-World Model to Assess Virtualization Platforms
  PDF: Intel Centrino Duo Processor Technology with Intel Core2 Duo Processor
Microsoft Article: Build and Run Virtual Machines with Hyper-V Server 2008
Go Parallel Article: Q&A with a TBB Junkie
IBM Whitepaper: Innovative Collaboration to Advance Your Business
Internet.com eBook: Real Life Rails
IBM eBook: The Pros and Cons of Outsourcing
Internet.com eBook: Best Practices for Developing a Web Site
IBM CXO Whitepaper: The 2008 Global CEO Study "The Enterprise of the Future"
Avaya Article: Call Control XML in Action - A CCXML Auto Attendant
IBM CXO Whitepaper: Unlocking the DNA of the Adaptable Workforce--The Global Human Capital Study 2008
Adobe Acrobat Connect Pro: Web Conferencing and eLearning Whitepapers
Symantec Whitepaper: Comprehensive Backup and Recovery of VMware Virtual Infrastructure
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
webref The latest from WebReference.com Browse >
Popular JavaScript Framework Libraries: An Overview - Part 3 · Accessing Your MySQL Database from the Web with PHP · Working with the DOM Stylesheets Collection
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Fixing MySQL Replication · Firewall Guide: First Steps to Securing the Enterprise · VoxOx Tames the Tumultuous Communications Tangle

Created: March 27, 2003
Revised: December 09, 2004

URL: http://webreference.com/programming/javascript/Jf/column71