A Couple of Quick jQuery Tips: Alternating Gray Rows in a Table, Highlight Table Row Mouseover

| No TrackBacks
alternating-rows-after.pngIn any web-application, there are always a few trivial and seemingly mundane enhancements the developer can implement that add significant value for the end-user.  And with the right tools in hand, implementing these enhancements can be down right simple.  Take alternating gray rows in a table for instance, or a slick mouseover highlighting effect when the user moves their mouse cursor over each row in a table.  Not surprisingly, you can easily implement these quick enhancements using jQuery.

A complete example can be found here, or continue reading for some screen shots.

Here's a very quick walk through ...

1 - Before

Here's what a boring table might look like before this enhancement:

alternating-rows-before.png


2 - After

Here's what the table would look like after the fix:

alternating-rows-after.png

Clearly this table is much more usable.  The background color of each row alternates and a yellow highlight follows the mouse cursor as the user navigates across the table.


3 - Example

A complete example, including the JavaScript and CSS can be found here.  In a nutshell, the supporting jQuery JavaScript is:

$("#mytbody > tr:even").addClass("even");
$("#mytbody > tr:odd").addClass("odd");

$("#myheaderrow ~ tr").bind( "mouseover", function(e){
$(this).toggleClass("highlight");
});
$("#myheaderrow ~ tr").bind( "mouseout", function(e){
$(this).toggleClass("highlight");
});

The supporting CSS and HTML examples can be found on the example page as well.

Enjoy.

Did You Find this Helpful?

Did you find this post helpful, or at least, interesting?

  

Send Mark a Direct Message

If you'd like to send me a direct message, please do so below. However, I do not publicly post comments or messages submitted directly to me. So, if you're going to try to SPAM me, or my blog, you're pretty much wasting your time.

400 characters remaining

Error

About Mark

A Silicon Valley native, Mark Kolich is a full-time Software Engineer, a casual entrepreneur, and a consultant for hire. A web technologies expert, his current focus is on building powerful and robust cloud-driven web-applications using Java, PHP, Perl, AJAX, DHTML, CSS, and JavaScript. His favorite programming languages are PHP, Java and JavaScript. He uses Linux, enjoys biking to work, loves building great software, and always writes elegant, readable, and maintainable code.

No TrackBacks

No trackbacks attached to this entry.

Twitter (@markkolich)

Translate

About this Entry

This page contains a single entry by Mark Kolich published on May 28, 2009 12:22 AM.

HOWTO: Tweet from the Command Line Like a Pro With Curl was the previous entry in this blog.

HOWTO: Using Java's SecureRandom (java.security.SecureRandom) is the next entry in this blog.

Find recent content on the main index or look in the archives to find all content.