Minneapolis based web design and development

A blog on design, usability, ajax, xhtml, css, and javascript

Table Row and Column Highlighting

February18

A current trend in data display is to now have row highlighting appear when you hover over any table cell. This is not a particularity daunting task, as it is a simple JavaScript class swap for each table row. But is there an easy way to highlight columns as well? The short answer is no, but with some basic DOM scripting the answer is yes.

Now what happens when you want to take this script to the next level? Let’s say I just wanted to highlight the leftmost cell and topmost cell of each cell that was hovered over. This again, becomes more complex with JavaScript.

I came up with the following scenarios for ways to highlight rows:

  • highlight entire row
  • highlight entire column
  • highlight only header cells
  • highlight active cell beneath the cursor differently
  • create a crosshairs effect
  • create a spotlight effect

All of these scenarios are completely independent of each other so they may be used in conjunction with the other. Also each function is abstracted out to the presentation layer to make for easy editing via stylesheets.

The code to apply table and row highlighting to your table is relatively easy:

<script type="text/javascript">
	/*
		Padding should be adjusted for cells that
		shouldn't have hover effects on them.
		initCrossHairsTable(id, top, right, bottom, left);
	*/
	initCrossHairsTable("crossTable", 1, 1, 1, 1);
</script>

That is all there is to applying the script.

Here is the demo of the script: Table Crosshairs.

Note that as it is written, the script can only be styled one way unless the JavaScript variables are overwritten on other pages.

Please remember that this script is only a rough draft on my idea. I don’t know if I ever plan on releasing a “prettier” version of the code. However as all developers know, we always want to go back and clean up the code later.

posted under JavaScript, DOM, CSS
3 Comments to

“Table Row and Column Highlighting”

  1. On February 19th, 2007 at 12:19 pm slider7 Says:

    this is exactly what i was looking for. would be nice if you cleaned up the code a little to make it work with multiple tables on one page. other than that, pretty cool script

  2. On April 16th, 2007 at 6:49 pm ScottDavis Says:

    This is spectacular! You’ve saved me hours and hours and hours…

  3. On August 16th, 2007 at 11:41 pm Ian Says:

    This is a nice script. Thanks for sharing what you know.

Email will not be published

Website example

Your Comment: