Adding Mouse Over Row Highlighting To Tables

Intro

I have a bit of “me” time today to write some code and I thought I’d treat myself to something light and fun by building something purely for its flashiness and aesthetic. Something that does’t have any useful purpose other than it looks good.

I am a big fan of adding effects to html pages by simply adding a reference to a separate javascript file. This keeps the javascript code separated from the HTML, which is a big help in avoiding “spaghetti script”.

This is a technique that Jon Galloway writes about in his post on Markup Based Javascript Effect Libraries which highlights several approaches for adding interesting behaviors to a page without using Flash or DHTML behaviors (which only work in IE anyways). By referencing a javascript file and adding certain semantic markup to html elements, an author can add very interesting effects to a page.

The Row Mouse Over Effect

The effect I will introduce is simple. Adding a javascript file and a couple CSS classes will allow you to add row highlighting to any table. It provides a mean to change the look and style of a row when you mouse over it, and then change it back when you mouse out.

All you need to do is to reference the tableEffects.js javascript file and add the “highlightTable” css class to a table. At that point, each row of the table will have its CSS class changed to “highlight” or “highlightAlt” when moused over. Its CSS class will be reset when the mouse leaves.

In order to actually see a change when you mouse over, you’ll have to style the rows for highlighting like so:

table.highlightTable tr.highlight td

{

    background: #fefeee;

}

table.highlightTable tr.highlightAlt td

{

    background: #fafae9;

}

The script assumes you want to use a different color for alternating rows. If not, you can simply style both highlight and highlightAlt the same.

See It In Action

col 1 col 2 col 3 col 4
Apple Orange Banana Kiwi
Pinto Porsche Peugot Acura
LAX SFO ANC NYC
No Body Look Here
Red Blue Green Alpha

Get the file

Download the file from here (Right click and save as).

If you have any improvements (as I am sure there will be some), please let me know and I will keep my version updated. I named the script “tableEffects.js” because I hope to add more interesting effects.

And remember, though I tend to preach table-less design, there are semantic uses for tables. When using this script, it helps to make sure that your tables are semantically marked up. For example, if you don’t want your header row to highlight, use <th> tags instead of <tr>. The script ignores the table header tags.

What others have said

Requesting Gravatar... Matthijs van der Vleuten Feb 05, 2006 1:37 PM
# re: Adding Mouse Over Row Highlighting To Tables
At least on my system, the highlight is barely visible.
Requesting Gravatar... Haacked Feb 05, 2006 5:52 PM
# re: Adding Mouse Over Row Highlighting To Tables
I've updated the styling so it's more noticeable.
Requesting Gravatar... Craig Feb 06, 2006 1:37 PM
# re: Adding Mouse Over Row Highlighting To Tables
I'm by no means a Javascript or HTML guy, but don't you want to use the same trick you use with onload to ensure that you don't whack an existing onmouseover or onmouseout event? But perhaps I'm displaying my ignorance.
Requesting Gravatar... Haacked Feb 06, 2006 1:50 PM
# re: Adding Mouse Over Row Highlighting To Tables
Ummm yeah. Actually you would. Unless you're a control freak and don't care what other effects were in place.
Requesting Gravatar... rolling May 10, 2006 9:12 AM
# re: Adding Mouse Over Row Highlighting To Tables
No way. Firefox 1.5 doesn't see anything.
There should be something wrong with your script.
Requesting Gravatar... anonymous person Jun 21, 2006 1:44 PM
# re: Adding Mouse Over Row Highlighting To Tables
Highlighting & 1.5.04 Firefox doesn't work for me either.
Requesting Gravatar... Ignatius Jun 23, 2006 3:20 AM
# re: Adding Mouse Over Row Highlighting To Tables
There are a mistake in the script. indexOf return -1 if no matches, or the position where the string is. This means that if we have only the 'highlightTable' class, indexOf('highlightTable') returns 0, because the string starts in the first character (0).

In your script appear [...]indexOf('highlightTable') > 0 but should be .indexOf('highlightTable') >= 0 (or != -1)

PD: my english sucks :)
Requesting Gravatar... Haacked Jun 23, 2006 10:51 AM
# re: Adding Mouse Over Row Highlighting To Tables
Ignatius, thanks for the correction! I swear it used to work, but I must've made a breaking change. I really need to get some unit tests for my javascript.
Requesting Gravatar... Robert Nguyen Sep 16, 2006 12:55 AM
# re: Adding Mouse Over Row Highlighting To Tables
Hi,

Could you please help me on this, I wrote a simple page to take advance your TableEfects.js
but id doesn't work and here is the code.

----
<html>
<head>
<title>WebForm1</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name=vs_defaultClientScript content="JavaScript">
<meta name=vs_targetSchema content="http://schemas.microsoft.com/intellisense/ie5">
<LINK href="help.css" type="text/css" rel="stylesheet">
<script language="JavaScript" type="text/JavaScript">
<!--
function initRowHighlighting()
{
if (!document.getElementsByTagName){ return; }

var tables = document.getElementsByTagName('table');

for(var i = 0; i < tables.length; i++)
{
var table = tables[i];
if(table.getAttribute('class') && table.getAttribute('class').indexOf('highlightTable') > -1)
{
//Make sure to use th tags for header row.
attachRowMouseEvents(table.getElementsByTagName('tr'));
}
}
}

function attachRowMouseEvents(rows)
{
for(var i = 0; i < rows.length; i++)
{
var row = rows[i];
if(i%2 == 0)
{
row.onmouseover = function()
{
this.setAttribute('oldClass', this.className);
this.className = 'highlight';
}

row.onmouseout = function()
{
this.className = this.getAttribute('oldClass');
}
}
else
{
row.onmouseover = function()
{
this.setAttribute('oldClass', this.className);
this.className = 'highlightAlt';
}

row.onmouseout = function()
{
this.className = this.getAttribute('oldClass');
}
}
}
}

//
// addLoadEvent()
// Adds event to window.onload without overwriting currently assigned onload functions.
// Function found at Simon Willison's weblog - http://simon.incutio.com/
//
function addLoadEvent(func)
{
var oldonload = window.onload;
if (typeof window.onload != 'function')
{
window.onload = func;
}
else
{
window.onload = function()
{
oldonload();
func();
}
}
}
addLoadEvent(initRowHighlighting);
//-->
</script>
</head>
<body MS_POSITIONING="GridLayout" onload="addLoadEvent(initRowHighlighting);">

<form id="Form1" method="post" runat="server" >


<table class="highlightTable">
<tr class="highlight">
<td>Sample 1</td>
</tr>

<tr class="highlightAlt">
<td>Sample 1</td>
</tr>
<tr class="highlight">
<td>Sample 1</td>
</tr>

</table>
</form>

</body>
</html>
----


Thanks

Robert Nguyen
Requesting Gravatar... Steve Harman Sep 19, 2006 3:39 PM
# re: Adding Mouse Over Row Highlighting To Tables
For those who might be interested...
in the spirit of the OSS movement, I thought I would share my new version of the "Mouse Over Row Highlighting Effect"... hence the Redux.

You can read more about it and grab the source here.
Requesting Gravatar... Drogfild Oct 24, 2006 6:06 AM
# re: Adding Mouse Over Row Highlighting To Tables
It seems that IE doesn't support method ".getAttribute('class')".

If you replace line "if(table.getAttribute('class') && table.getAttribute('class').indexOf('highlightTable') > -1)" with "if(table.className && table.className.indexOf('highlightTable') > -1)" it seems to work with IE also.

--
Drogfild
Requesting Gravatar... goog Dec 25, 2006 9:55 PM
# re: Adding Mouse Over Row Highlighting To Tables
good work
Requesting Gravatar... phill May 02, 2007 7:39 PM
# re: Adding Mouse Over Row Highlighting To Tables
Nice work man.
I totally agree, table-less design does not mean a ban on all tables. Semantic uses for tables still exist.
Thanks for the script.
Requesting Gravatar... Josh Mitchell Dec 14, 2007 2:33 PM
# re: Adding Mouse Over Row Highlighting To Tables
This is awesome! I'd like to use this on a webform I'm creating -- how hard would it be to convert this so that the highlighting happens when a form field within a row is clicked instead? I don't have a huge amount of programming background, so if you have an example, it would be greatly appreciated!

Many thanks,
Josh

What do you have to say?

(will show your gravatar)
Please add 5 and 8 and type the answer here: