Walking through the simple code needed to stripe table rows.

Original: http://15daysofjquery.com/table-striping-made-easy/5/
Author: Jack Born

Similar Tutorials: TraversingSelectorsCSSEvents

I've prepared a step by step demonstration to take someone else's well written JavaScript and reduce it down to 5 lines or less. At the same time you'll see how I make it a little more flexible and useful.

Take a look at "Stripe your tables the OO way" by Matthew Pennell. His code is clean and concise. And it's an improvement over a past article from A List Apart.

But with the help of jQuery, I think we can do better.

The concept is to create stripes on a table and then have the background of each row change color when your cursor rolls over.

Image:Zebra1.png

Contents

[edit]

The Goal

As I mentioned, Mathew's code is quite good. It shows a level of skill that most part-time web geeks might find difficult to attain. It's certainly above the head of someone just starting to roll up their sleeves and learn JavaScript.

How can jQuery help?

My mission is to show you that jQuery can make this task:

So what are you waiting for? Jump into the tutorial and see how easy jQuery makes table striping.


[edit]

Starting Point

Image:Zebra2.png

[edit]

CSS To Pay Attention To

Image:zebra3.png

[edit]

One Minor Change

Image:zebra2-b.png

[edit]

First Steps With jQuery

Image:zebra4-b.png

[edit]

The Basics - Made Simple

Image:zebra5.png

[edit]

Alternating Rows Is Easy

Image:zebra6.png

Now our table is striped

[edit]

Quick Recap

Image:zebra7.png

Image:zebra7-b.png

[edit]

Highlight Row As Cursor Rolls Over

Image:zebra8.png

[edit]

Add A Class When Mouseover Triggered

Image:zebra9.png

[edit]

Mouseout

Image:zebra10.png

[edit]

The Finished Product

Lorem Ipsum Dolor Sit Amet
Lorem Ipsum Dolor Sit Amet
Lorem Ipsum Dolor Sit Amet
Lorem Ipsum Dolor Sit Amet
Lorem Ipsum Dolor Sit Amet
Lorem Ipsum Dolor Sit Amet
Lorem Ipsum Dolor Sit Amet
Lorem Ipsum Dolor Sit Amet
Lorem Ipsum Dolor Sit Amet
Lorem Ipsum Dolor Sit Amet
Lorem Ipsum Dolor Sit Amet

[edit]

Did I Goof?

Image:zebra11.png

[edit]

Chainable Methods

Image:zebra12.png

[edit]

Pile It On One Line If You Want

 <script src="jquery.js"></script>
 <script type="text/javascript">
 $(document).ready(function(){
   $(".stripeMe tr").mouseover(function(){$(this).addClass("over");}).mouseout(function(){$(this).removeClass("over");});
   $(".stripeMe tr:even").addClass("alt");
 });
 </script>
[edit]

Why Use jQuery?

[edit]

A limitation and a workaround