views:

332

answers:

3

I have a table that i want to "highlight" during onmouseover/onmouseout. I already know this is required in IE but not in other browsers.

I have managed to detect the events triggering and this TR tag effectively works. (Note that the originating class "contentTableRow" doesn't seem to be causing any issues.)

class="contentTableRow" onclick="openForm('SomeID');" onmouseover="highlight('someRowID', true);" onmouseout="highlight('someRowID', false);" id="someRowID" 

All is fine and dandy, the "highlight" function fires and actually sets the appropriate class.

It's just that IE won't process the CSS class name change.

Here is a snippet of the CSS I am using to make the change.

.HighlightOn {
    cursor:pointer;
    background-color: #D1DFFF;
}

.HighlightOff {
    background-color: #E1EEFE;
}

I can see that the Class names are getting updated when I debug it, and also check it in Firebug. But it seems that IE doesn't like this usage of classes with a TR tag.. Is it the way I am structuring the class for Tables ? Any advice ?

+3  A: 

Are you changing class instead of className? class is reserved in Javascript as the actual class declaration keyword, so the property is called className:

function highlight(id, b) {
    document.getElementById(id).className = (b ? "HighlightOn" : "HighlightOff");
}

Incidentally, you might just want to pass "this" to highlight instead of the id, so it doesn't need to do the document.getElementById() call

Michael Mrozek
This is some nice code. Very clean. But the problem seems to be related to the way IE processes the CSS.I can see the class is assigned, and CSS is "sort of" being recognised because the "cursor" setting is working. Just not the background color update.. Weird.. but then again, it is IE.
giulio
I up'd your comment for the info.. thanks
giulio
+1  A: 

IE won't recognize "class" in JavaScript. You must use "className" as the property in IE.

Jeff Fohl
Yep.. Thanks jeff. But that's not the issue.
giulio
A: 

Thanks for all the pointers. But this seems to have worked.

TR.HighlightOn td {
    cursor:pointer;
    background-color: #D1DFFF;
}

TR.HighlightOff  td {
    cursor:pointer;
    background-color: #E1EEFE;
}

Basically have to be explicit in this case about where the class is used in the HTML.

Note that I had to reference the TR tag and the TD tags relative to where I am using the Highlighton/off classes in the table. Thanks jensgram.

Hope this helps anyone else with the same problem.

(thanks Jensgram for the lead)

giulio