Advanced CSS Tables II – Using Mootools JavaScript For Alternate Row Colors

By  on  

As I discussed in Advanced CSS Tables - Using CSS3 For Alternate Row Colors, we will eventually be able to use the ":nth-child(argument)" pseudo-class in CSS3 to provide alternate row background colors. What do we use in the mean time? You can explicitly code your page using CSS classes to do the job, but that's far too painful if you aren't using server-side scripting. Even if you do use server-side scripting, your page could become quite bloated from rows and rows of "class='color-me'" in your code. I've put together a very simple MooTools snipped that will save you lots of programming time and download time for your users.

Adding the Code

There are three parts to the process: the CSS, the JavaScript, and the XHTML table we will create.

The CSS is rather easy. We'll create one class for the table (which will serve as an identifier -- we may not want all tables on the page to be shaded, right?) and two classes for the odd and even row formatting:

.shade-table	{ /* just to remind us we're using this -- we don't want to write over it later! */ }
.odd		{ background:#ccc; padding:3px; }
.even		{ background:#eee; padding:3px; }

Now that we've defined the CSS, we need to insert the Mootools JavaScript code into the <head> section of the page:

window.addEvent('domready', function() {
	var count = 0;
	$$('table.shade-table tr').each(function(el) {
		el.addClass(count++ % 2 == 0 ? 'odd' : 'even');
	});
});

Now we're set. The only remaining step is place the table with the designated class declaration to the body of the page:

<table class="shade-table" border="1" cellpadding="5" cellspacing="2"><tbody>
<tr>
<td>1</td>
<td>1</td>
<td>1</td>
<td>1</td>
</tr>
<tr>
<td>2</td>
<td>2</td>
<td>2</td>
<td>2</td>
</tr>
<tr>
<td>3</td>
<td>3</td>
<td>3</td>
<td>3</td>
</tr>
<tr>
<td>4</td>
<td>4</td>
<td>4</td>
<td>4</td>
</tr>
<tr>
<td>5</td>
<td>5</td>
<td>5</td>
<td>5</td>
</tr>
</tbody></table>

Voila! Mootools' easy function make the entire process a snap. The only downside to this method is that a user with JavaScript disabled wont see the newly added background colors. It could be worth it to you to save the bandwidth depending on your website's audience.

Recent Features

  • By
    Camera and Video Control with HTML5

    Client-side APIs on mobile and desktop devices are quickly providing the same APIs.  Of course our mobile devices got access to some of these APIs first, but those APIs are slowly making their way to the desktop.  One of those APIs is the getUserMedia API...

  • By
    Create a CSS Flipping Animation

    CSS animations are a lot of fun; the beauty of them is that through many simple properties, you can create anything from an elegant fade in to a WTF-Pixar-would-be-proud effect. One CSS effect somewhere in between is the CSS flip effect, whereby there's...

Incredible Demos

  • By
    MooTools Kwicks Plugin

    I wrote a post titled Get Slick with MooTools Kwicks ages ago. The post was quite popular and the effect has been used often. Looking back now, the original code doesn't look as clean as it could. I've revised the original...

  • By
    Prevent Page Zooming in Mobile Browsers

    Ever since I got my iPhone, I've been more agreeable in going places that my fiancee wants to go. It's not because I have any interest in checking out women's shoes, looking at flowers, or that type of stuff -- it's because my iPhone lets...

Discussion

  1. You don’t need count. Array::each has a second argument, the index (and a third, the Array itself).

    MooTools 1.11:

    $$(’table.shade-table tr’).each(function(el, i) {
        el.addClass((i % 2) ? ‘even’ : ‘odd’);
    });
    

    MooTools 1.2 (dev)

    $$(’table.shade-table tr:odd’).addClass(‘odd’);
    
  2. Thank you for your post and insights digitarald! Taking the count var out of play will make the code a little cleaner and easier.

    I’ve enjoyed your work with MooTools and continued success!

  3. Puneet Madaan

    you don’t need that heavy increment loop.. All new Browsers have inbuilt capability for this.

    in mootools way, a easy way to achived by..

    $$('tr:even').setStyle('background-color', '#ccc');
    $$('tr:even').setStyle('background-color', '#eee');
    
  4. Puneet Madaan

    anyways, this site offers great resources :) will visit it often :)

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!