/    Sign up×
Community /Pin to ProfileBookmark

saving/getting class variables – className not working

Hi, this is my first post – so hello. I’m a long time member of evolt.org’s “thelist” but I wanted a forum specifically for JS and this looks good.

Anway, my problem is this:

I have a table of data that is styled with various background colors. Any one cell can be one of 5 colors – 2 shades of yellow, 2 shades of green or black.

I have a function where you rollover the row, I highlight that row so it is more easily readable.

PROBLEM: Highlighting the row is easy, returning each cell back to its’ one of 5 colors is not so easy. This is what I want to do.

Here is my highlight function (which is set via an init() function
during onload):

[code=php]
function HLRow()
{
var table = document.getElementById(“schedTable”);
var oRows = table.getElementsByTagName(“TR”);

var lastRowClass;

for(var i=0; i<oRows.length; i++)
{
oRows[i].onmouseover = function() {
BoldRow_HLCells(this);
this.style.fontWeight = “bold”;
this.style.color = “#cc0000”;
}
oRows[i].onmouseout = function() {
BoldRow_ResetCells(this);
this.style.fontWeight = “normal”;
this.style.color = “#000”;
}
}
}
[/code]

You can see I am calling 2 other functions within, one to highlight the row, one to reset the row back to its colors – that’s where I need the help. Those two functions are nearly identical:

[code=php]

var classArray = new Array();
function BoldRow_HLCells (oRow) {
var oCells = oRow.getElementsByTagName(“TD”);
for(var i=0; i<oCells.length; i++)
{
classArray[i] = oCells[i].className;
oCells[i].style.backgroundColor=”#ccffcc”;
}
}

function BoldRow_ResetCells (oRow) {
var oCells = oRow.getElementsByTagName(“TD”);
for(var i=0; i<oCells.length; i++)
{
oCells[i].className = classArray[i]; // doesn’t work!
}
}
[/code]

The problem is trying to go back and RESET them based on the values of my array (values which test as correct) doesn’t work. For some reason resetting the className is not working in either IE6 or NS7. Why the array value for the class does not get assigned is beyond me. I’ve done this in many other variations before with no problem, and it is certainly supported. But I have heard that className can get quirky.

So how do I set my values for the TDs back to the originals?

You can see the page in its full glory at:

[url]http://www.pixelmech.com/review/newWorkerTable.html[/url]

TIA

Tom

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@dragleNov 13.2003 — Hi Tom,

If you set a style attribute directly, it will override the class settings for the element (in the same manner it would if you assigned an inline style definition with the element itself). Resetting the class at a later time will not, in and of itself, override your original override. So in your original highlight, when you do this:
<i>
</i>oCells[i].style.backgroundColor="#ccffcc";

That background color remains stuck on the item when you later change the class.

A couple possibilities come to mind. On your mouseout set the style.backgroundColor to empty, which should force the browser to again use the class setting for the entry; or use a class entry to change the item originally on mouseover (i.e., avoid the direct bgcolor setting entirely). I know both of these techniques will work for the latest browsers, but you'll need to test around a bit for compatibility with others.

HTH,
Copy linkTweet thisAlerts:
@pixelmechauthorNov 14.2003 — As you and someone else suggested, the solution was:

[code=php]
oCells[i].style.backgroundColor="";
[/code]


Which reset the cell to look at the already applied class. Thanks for your input. ?

Tom
Copy linkTweet thisAlerts:
@dragleNov 14.2003 — You're welcome!
×

Success!

Help @pixelmech spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...