Welcome to the educational series that shows how to build zebra tables with CSS and JavaScript. In this third tutorial of the series I’m going to show you how to improve the signature of the JavaScript function defined in the last article. This will make it suitable for working with tables that include different <tbody> sections.
An Improved Approach to Building Zebra Tables - Setting up a final hands-on example (Page 4 of 4 )
In the prior section, I showed you how to improve the signature of the pertinent “buildZebraTable()” JavaScript function to provide it with the ability to build zebra tables that include multiple <tbody> sections. However, the function itself can look rather unintelligible if it’s not linked with a target (X)HTML table, as well as the corresponding CSS styles.
Thus, below I included the complete source code of a brand new (X)HTML file, which demonstrates how to use the recently-modified function to build a basic zebra table.
That being said, here’s how this sample file looks:
<!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 above (x)HTML file includes a regular table that contains three different <tbody> sections. Nevertheless, the “buildZebraTable()” JavaScript function perfectly adapts itself to these brand new circumstances. Once the pertinent web document has been loaded, it iterates over the table in question and automatically turns it into a zebra element!
Finally, to complement the previous practical example, I included a screen shot that shows the visual appearance of the zebra table just constructed. Here it is:
Now that you've hopefully grasped the logic that drives the construction of zebra tables with JavaScript and CSS, I suggest you use all of the code samples developed in this tutorial and build your own test examples.
Final thoughts
In this third installment of the series, I showed you how to create a simple JavaScript function aimed at building zebra tables that include multiple <tbody> sections within their corresponding markup. As you saw for yourself, the whole process is very comprehensive.
Nonetheless, there’s one topic that remains uncovered with reference to building zebra tables. As you’ve surely noticed, the JavaScript function defined previously uses two different CSS classes to alternately style the even and odd rows of the selected table. However, it’s also possible to utilize the “style” (yet non-standard) object to directly manipulate the background color of each row.
This alternative technique will be covered in depth in the last article of the series, so you don’t have any excuses to miss it!
DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.