Forum Moderators: open

Message Too Old, No Replies

ClassName problem

Can't work out why it has no effect

         

thesheep

9:02 pm on Aug 17, 2005 (gmt 0)

10+ Year Member



Been working a long time today and probably I'm missing something basic. I have a function that changes the classname of an element so that it changes colour.

function change()
{
if (!document.getElementById) return null;
document.getElementById('lala').ClassName = 'test';
alert('The classname of lala is ' + document.getElementById('lala').ClassName);
}

----------

<p id="lala">Text</p>
<a href="#" onClick="change()">change</a>

If I set the class directly on the <p> element, the style shows up fine. But if I click the link, I get the alert OK, but no style change! What am I doing wrong?

ChadSEO

9:38 pm on Aug 17, 2005 (gmt 0)

10+ Year Member



thesheep,

Looks like the problem is with the capitalization on ClassName. The proper format is "className", with no capital C. Essentially what you are doing is setting a new variable to test, and then printing that. So the correct function should be:

function change()
{
if (!document.getElementById) return null;
document.getElementById('lala').className = 'test';
alert('The classname of lala is ' + document.getElementById('lala').className);
}

Chad

thesheep

9:49 pm on Aug 17, 2005 (gmt 0)

10+ Year Member



Brilliant, thanks.

thesheep

9:57 pm on Aug 17, 2005 (gmt 0)

10+ Year Member



Actually now it works fine on Firefox but not IE6. Probably need to recheck the code again.

ChadSEO

10:02 pm on Aug 17, 2005 (gmt 0)

10+ Year Member



When I was testing it, I got it to work in both Firefox and IE, so yeah, look through your code again, and if you still can't get it to work, just post again :)

Chad