This articles series covers some useful basic jQuery techniques: coloring alternate table rows, validating target blank links, creating collapsing boxes with a slide in/out effect, and other interesting features you can add to your site. I am going to mix up semantic HTML, with simple CSS, and my library of choice - jQuery, to achieve these results. If you use another library than jQuery this article may still be helpful, because it shows you the detailed process to reach the particular goal.
The technique of this week is very simple and easy to learn. You’re going to learn how to add a zebra-style to your tables, just like iTunes:

Lengthy tables often lead to a boring look-and-feel, and confusing readers when the rows gets too wide and numerous. You can make reading a lot easier by using alternate colored rows.
The logic behind coloring alternate table rows is: use JavaScript (in our case jQuery, but you can do it in your own way) to filter even rows and add a specific CSS class to it, and filter the odd and add the other CSS class. You can do this filtering / sorting with server-side scripting, but today we’re going to use simple client-side to handle that.
Accordingly to best practices and accessibility standards, the HTML markup should be semantic, which means a readable header, body, and optional footer describing the table. There’s an excellent explanation of accessible tables here. Our table is simpler, so we are going to use a shorter, but still accessible markup. That means:
- <table class=”zebra”>
- <thead>
- <tr>
- <th>Song</th>
- <th>Length</th>
- </tr>
- </thead>
- <tbody>
- <tr>
- <td>Song 01<td>
- <td>5:57<td>
- </tr>
- <tr>
- <td>Song 02<td>
- <td>4:31<td>
- </tr>
- <tr>
- <td>Song 03<td>
- <td>9:12<td>
- </tr>
- <tr>
- <td>Song 04<td>
- <td>6:40<td>
- </tr>
- <tr>
- <td>Song 05<td>
- <td>0:57<td>
- </tr>
- <tbody>
- </table>
Easy. The next step is to choose the colors for even and odd rows. In our example, we are going to use the same colors provided in iTunes: #FFF and #F4F7FB. The CSS will be:
- table.zebra tr.even td, table.zebra tr.even th { background-color:#FFF; }
- table.zebra tr.odd td { background-color:#F4F7FB; }
The final step is to filter even and odd rows using jQuery selector filters:
- $( function(){
- $(”table.zebra tr:even”).addClass(”even”);
- $(”table.zebra tr:odd”).addClass(”odd”);
- });
In this fragment, we said to jQuery to do the magic of filtering “:even” rows - beginning with the zero index and add the .even CSS class. Then we said it also to filter the “:odd” rows ( the remaining ones ) and add the .odd. I also used a CSS class to tell jQuery which table it is going to filter: .zebra, because I didn’t want all tables on the page to be styled.
That’s it. Alternating CSS styled tables is easy this way. Instead of “zebra”, se your imagination to give better names to your classes
5 comments
Leave your comment
Your comment is encouraged. Please be polite, and keep your ideas on topic. Please do not spam. Innadequate comments will be deleted.
The * means required fields.
WebamsterFebruary 2nd, 2008 at 7:55 pm
Woaw it’s great … but it would greater with the code to implement with jquery and the function in details. why do you use ‘$’ to initialize your function? A demo would be cool to understand how it works too. take care. Greg
RickFebruary 2nd, 2008 at 9:53 pm
hi Greg,
please read this page on jQuery docs: http://docs.jquery.com/How_jQuery_Works#Launching_Code_on_Document_Ready
about the demo, this page itself uses this alternating pattern.. the code samples background are zebra-style
AdamMarch 11th, 2008 at 5:02 pm
I have been unable to get this to work. I pasted the javascript into the head of the same html doc that contains the table and the css. Any ideas?
RickMarch 11th, 2008 at 5:31 pm
Adam, could you please upload your code for us to help you?
JacobMarch 24th, 2008 at 8:53 am
Adam,
If you’ve cut and pasted directly from the above, you’ll have copied the ” character (double backtick) instead of ” (double quote) - very small difference, but it will cause the code to not work (as I just discovered).
Getting the Firebug extension for Firefox will definitely help you with problems like this
Cheers,
Jacob