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
    Page Visibility API

    One event that's always been lacking within the document is a signal for when the user is looking at a given tab, or another tab. When does the user switch off our site to look at something else? When do they come back?

  • By
    Introducing MooTools Templated

    One major problem with creating UI components with the MooTools JavaScript framework is that there isn't a great way of allowing customization of template and ease of node creation. As of today, there are two ways of creating: new Element Madness The first way to create UI-driven...

Incredible Demos

  • By
    Spoiler Prevention with CSS Filters

    No one likes a spoiler.  Whether it be an image from an upcoming film or the result of a football match you DVR'd, sometimes you just don't want to know.  As a possible provider of spoiler content, some sites may choose to warn users ahead...

  • By
    dwProgressBar v2:  Stepping and Events

    dwProgressBar was a huge hit when it debuted. For those of you who didn't catch my first post, dwProgressBar is a MooTools 1.2-based progress bar which allows for as much flexibility as possible. Every piece of dwProgressBar can be controlled by CSS...

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!