Using the Style Object for Zebra Tables with CSS and JavaScript
Welcome to the final chapter of the series "Building Zebra Tables with CSS and JavaScript." In this article, I’ll teach you how to build zebra tables by using the popular “style” object, thus completing this educational four-part guide.
Using the Style Object for Zebra Tables with CSS and JavaScript - Building zebra tables with JavaScript (Page 2 of 4 )
Before I proceed to show you how to use the “style” JavaScript object, first I’d like to list the complete source code of the web application that I developed in the last tutorial. It was capable of building the same type of tables, but in this particular case via the utilization of two different CSS classes.
That being said, have a quick look at the signature of the following (X)HTML file, which implements the JavaScript-based approach described above in order to build a primitive zebra table.
The corresponding code sample is as follows:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<h1>Example on building a simple zebra table with CSS and JavaScript (improved version)</h1>
<table id="zebratable">
<tbody>
<tr>
<td><p>Content for cells goes here</p></td>
</tr>
<tr>
<td><p>Content for cells goes here</p></td>
</tr>
<tr>
<td><p>Content for cells goes here</p></td>
</tr>
<tr>
<td><p>Content for cells goes here</p></td>
</tr>
<tr>
<td><p>Content for cells goes here</p></td>
</tr>
</tbody>
<tbody>
<tr>
<td><p>Content for cells goes here</p></td>
</tr>
<tr>
<td><p>Content for cells goes here</p></td>
</tr>
<tr>
<td><p>Content for cells goes here</p></td>
</tr>
<tr>
<td><p>Content for cells goes here</p></td>
</tr>
<tr>
<td><p>Content for cells goes here</p></td>
</tr>
</tbody>
<tbody>
<tr>
<td><p>Content for cells goes here</p></td>
</tr>
<tr>
<td><p>Content for cells goes here</p></td>
</tr>
<tr>
<td><p>Content for cells goes here</p></td>
</tr>
<tr>
<td><p>Content for cells goes here</p></td>
</tr>
<tr>
<td><p>Content for cells goes here</p></td>
</tr>
</tbody>
</table>
</body>
</html>
As you can see, the previous hands-on example uses a simple JavaScript function called “buildZebraTable().” It comes in handy for styling the respective even and odd rows of a selected table. Of course, it’s worthwhile to highlight a detail that’s very important: in this specific case, the mentioned JavaScript function constructs the zebra table via a pair of CSS classes, which are assigned alternately to each table row. This is a process that sticks to the standards dictated by the W3 Consortium.
But is there any way to build a zebra table, other than working with CSS classes? The answer is an emphatic yes! As I suggested in the introduction, it’s possible to use the “style” JavaScript object to directly manipulate the background colors of the zebra table being constructed. Naturally, I have to mention again that this object isn’t a standard one, but the technique deserves a closer look.
Therefore, in the course of the following section, I’m going to show you how to utilize the“style” JavaScript object in order to create zebra tables with minor hassles.