Table Row-Cell Highlighter
September 25th, 2008

This script is used to highlight a table cell and optionally, the row which contains it.

Table row-cell highlighter demo

Place the following code in a custom header:

<script type="text/javascript">
//---------------- Begin User Settings -------------------------------
var cellColor = "#cccccc";  // This sets the cell color
var rowColor = "aqua";      // This sets the row color
var useRows = true;         // This determines if rows are highlighted
//---------------- End User Settings ---------------------------------
 
var origColor;
 
function markCells(tableObj) {
  var div = document.getElementById('O'+tableObj)
  var tbl = div.getElementsByTagName("table");
  var trs = tbl[0].getElementsByTagName("tr");
 
  for (var i=0; i<trs.length; i++) {
    tds = trs[i].getElementsByTagName("td");
    for (j=0; j<tds.length; j++) {
      tds[j].onmouseover = hiliteCell;
      tds[j].onmouseout = retCell;
    }
  }
}
 
function hiliteCell() {
  origColor = this.style.backgroundColor;
  if (useRows) {
    this.parentNode.style.backgroundColor = rowColor;
  }
  this.style.backgroundColor= cellColor;
}
 
function retCell() {
 this.style.backgroundColor= origColor;
  if (useRows) {
    this.parentNode.style.backgroundColor = origColor;
  }
}
</script>

There are three user defined variables used in the script:

cellColor This defines the background color applied to the table cell when the mouse passes over it. The value is an HTML 6-digit hex color code, or a known color name
rowColor This defines the background color applied to the row containing the table cell when the mouse passes over it. The value is an HTML 6-digit hex color code, or a known color name
useRow This determines if rows are to be highlighted. If the value is set to true, the row will be highlighted along with the cell. If set to false, only the table cell is highlighted

The following rules are applied when the script is used:

  1. If a cell object already has a background color, it will not be altered by the script. When you use the SiteSpinner shading tool to give your object a background color, it overrides any background setting given to the table cell.
  2. If the cell is given a background color, the script will retain the original color when the mouse enters the cell, and return the cell to its normal color when the mouse leaves the cell.
  3. If row highlighting is enabled, the highlight color will only appear in cells that contain no background color


Leave a Reply

Spam protection by WP Captcha-Free


May 24th, 2012