8f5553306c2cf7f4b14153f6117f8e9b

A fairly common thing to do. Wondering if there's a more efficient way.

document.observe('dom:loaded', function() {
    var tObjs = document.getElementsByTagName('table');
    for (var x = 0; x < tObjs.length; x++) {
       if(tObjs[x].className.match('highlight')) {
          oTrs = tObjs[x].getElementsByTagName('tr');
          for (var y = 0; y < oTrs.length; y++) {
             oTrs[y].onmouseover = function() { colorRow(this) };
             oTrs[y].onmouseout = function() { returnRow(this) };
          }
       }
    }
});
function colorRow(e) {
    e.activeElement = $(e);
    e.addClassName('on');
}
function returnRow(e) {
    e.activeElement = $(e);
    e.removeClassName('on');
}

Refactorings

No refactoring yet !

A8d3f35baafdaea851914b17dae9e1fc

Adam

May 26, 2009, May 26, 2009 15:15, permalink

1 rating. Login to rate!
document.observe('dom:loaded', function() {
    $$('table.highlight tr').each(function(element) {
        element.activeElement = element;
        element.onmouseover = element.addClassName.curry('on');
        element.onmouseout = element.removeClassName.curry('on');
    });
});
8f5553306c2cf7f4b14153f6117f8e9b

Danny Peck

May 26, 2009, May 26, 2009 15:37, permalink

No rating. Login to rate!

Ah nice Adam. Thanks. Curious, though, why one would just not do this?

document.observe('dom:loaded', function() {
             $$('table.highlight tr').each(function(element) {
               element.onmouseover = element.addClassName.curry('on');
               element.onmouseout = element.removeClassName.curry('on');
             });
         });
A8d3f35baafdaea851914b17dae9e1fc

Adam

May 26, 2009, May 26, 2009 18:30, permalink

No rating. Login to rate!

I wasn't sure why you were setting activeElement, so I left it in for completeness.

8f5553306c2cf7f4b14153f6117f8e9b

Danny Peck

May 26, 2009, May 26, 2009 19:59, permalink

No rating. Login to rate!

Cool. thanks again. Yeah sometimes IE freaks out if you don't set activeElement for reasons I'm not sure of.

254c76877dc8f0406ff8dadd275c4d82

NTulip

July 24, 2009, July 24, 2009 15:39, permalink

No rating. Login to rate!

any reason why you won't do it with just CSS? Given the example code here.

.your-class {
	font-size: 12px;
	margin: 0px;
	text-align: left;
	border-collapse: separate;
	border-bottom:none;
}
.your-class th {
	font-size: 13px;
	font-weight: bold;
	padding: 1px;
	background: #EFEFEF;
	border-top: 1px solid #FFF;
	color: #333;
	text-align: left;
}
.your-class td {
	padding: 1px;
	background: none; 
	border-top: 1px solid #CCC;
	color: #666;
	border-bottom: none !important;
}
.your-class tr:hover td {
	background: #FBFBFB;
	color: #333;
}
.your-class tr.footer { background: none !important; }
.your-class tr.footer:hover td { background: none !important;  }
04b942ca7001ac7179aa14bb3d590878

Mike

September 24, 2009, September 24, 2009 02:47, permalink

No rating. Login to rate!

I know we don't like IE and we should use it, but IE doesn't support hover on any element except <a>

04b942ca7001ac7179aa14bb3d590878

Mike

September 24, 2009, September 24, 2009 02:49, permalink

No rating. Login to rate!

Rectifying: We **shouldn't** use IE!

Your refactoring





Format Copy from initial code

or Cancel