Table mouseover highlighting with jQuery and event bubbling

This is a demonstration of one of the jQuery performance tips mentioned in Dave Artz's blog post, jQuery Performance Rules, specifically the delegation of event handling to nodes higher in the DOM, or event bubbling. In this example, I've attached mouseover and mouseout event handlers to the table below which toggle a class on both the cell and the row being moused over or out of. This approach saves your code from having to walk through the entire table attaching the equivalent event handlers to each node individually.

If you view the source of this page, this is the block of JavaScript that's doing all the work:

$(function(){
    $('#table1').mouseover(function(e){
        $(e.target).addClass('highlight').parent().addClass('highlight');
    }).mouseout(function(e){
        $(e.target).removeClass('highlight').parent().removeClass('highlight');
    }); 
});

« Back to g9g.org

Median Age of the Total Population (geographies ranked by estimate)

Data Set: 2008 Population Estimates

Source: US Census Bureau, Population Estimates Program More Tables and Information: Population Estimates Program
Rank Geographic area Median age
  United States 36.8
     
Maine 42.0
Vermont 41.2
West Virginia 40.6
4t Florida 40.2
4t New Hampshire 40.2
Pennsylvania 39.9
Connecticut 39.4
Montana 39.3
Rhode Island 38.8
10  New Jersey 38.7
11  Massachusetts 38.6
12t Delaware 38.2
12t Wisconsin 38.2
14t Iowa 38.1
14t Ohio 38.1
16t Hawaii 38.0
16t Michigan 38.0
16t New York 38.0
16t Oregon 38.0
20t Kentucky 37.7
20t Maryland 37.7
20t Tennessee 37.7
23  South Carolina 37.6
24t Alabama 37.5
24t Missouri 37.5
26t Minnesota 37.3
26t South Dakota 37.3
28t Arkansas 37.2
28t Washington 37.2
30t North Dakota 37.1
30t Virginia 37.1
32  North Carolina 36.9
33  Wyoming 36.8
34  Indiana 36.7
35t Kansas 36.2
35t Nebraska 36.2
37  Oklahoma 36.1
38  Illinois 36.0
39  Nevada 35.9
40  New Mexico 35.8
41  Colorado 35.7
42  Louisiana 35.6
43  Mississippi 35.3
44  Arizona 35.1
45t District of Columbia 34.9
45t Georgia 34.9
47  California 34.8
48  Idaho 34.4
49  Alaska 33.3
50  Texas 33.2
51  Utah 28.7
     
  Puerto Rico 35.8