An Improved Approach to Building Zebra Tables
(Page 1 of 4 )
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.
Over the years, zebra tables have become one of the preferred elements of many web designers, especially when it comes to displaying tabular data in a clear and professional fashion. This trend seems to be growing steadily over time. However, the most exciting aspect of these fancy tables is that they can be built using different approaches, including both client and server-side scripting.
However, when a zebra table is constructed manually by utilizing a few CSS styles to alternate the background colors of its respective odd and even rows, not only can this process be quite annoying, but it may lead to errors in the table’s (X)HTML markup, especially if the table in question is comprised of numerous rows.
In this particular case, JavaScript can be used to automate the styling process on the table. But as you may guess, learning the details of how to implement this approach requires some time and practice as well. Nonetheless, if you start reading this article series, you’ll be provided with numerous hands-on examples concerning the creation of zebra tables by utilizing clean and unobtrusive JavaScript code. You can use them to improve the visual presentation of your own web site’s tabular data.
Having introduced the primary objective of this group of articles, let me review a couple of topics that were covered in the preceding tutorial. As you’ll surely recall, in the last installment I explained how to automate the styling process for the even and odd rows of a selected table in order to provide it with the so-called “zebra” appearance.
Naturally, this procedure was performed by way of a short JavaScript function that was tasked with alternately assigning two different CSS classes to the respective table’s rows. This implemented a simple, yet powerful, client-side approach that is handy for dynamically building different zebra tables. Now, does this technique ring any bells? I bet it does!
At this point, I’m reasonably sure that you’ve already recalled the key concepts that surround the creation of zebra tables with CSS and JavaScript. Thus, it’s the appropriate time to move forward and continue digging deeper into this interesting subject.
Let’s continue this educational journey now!
Next: Constructing zebra tables dynamically >>
More JavaScript Articles
More By Alejandro Gervasio