...

View Full Version : I need to change a CSS class with javascript



sergimo
10-29-2004, 12:37 AM
I'd like to know if it's possible to change the CSS class of a link when clicked using javascript, cause i need to change its backround image, font-weight, etc.
I really appreciate your help. Thanx.

Sergimo

liorean
10-29-2004, 12:49 AM
<a ... onclick="this.className='new_class';">...</a>

sergimo
10-29-2004, 12:57 AM
I'm new with javascript so I need the complete function, thanx again.

liorean
10-29-2004, 01:13 AM
If all you want to do is to change the class of the link, the code I gave you is enough. Just have the changes you want in your style sheet.

SpiritualStorms
10-29-2004, 03:43 AM
from sergimo:

I'd like to know if it's possible to change the CSS class of a link when clicked using javascript, cause i need to change its backround image, font-weight, etc.
I really appreciate your help. Thanx.

Sergimo

Did you actually mean change the whole class? Or do you mean simply to change certain attributes of a class. You do realise that a class is generally a property, and it is a property by which you connect a particular tag with a particular CSS definition? It doesnt sound to me like you are making a distinction between a class as a property, and an actual CSS attribute.

liorean
10-29-2004, 03:48 AM
SpiritualStorms: You got it the wrong way around:

-HTML and XML elements have attributes.
-CSS rule sets have properties.
-Both of those are distinguished from the properties in JavaScript and the DOM.

SpiritualStorms
10-29-2004, 03:54 AM
from liorean:

SpiritualStorms: You got it the wrong way around:

-HTML and XML elements have attributes.
-CSS rule sets have properties.
-Both of those are distinguished from the properties in JavaScript and the DOM.


Of the first 2, i would say you are only arguing semantics. But ok, so maybe i wasnt totally precise in my labeling of terms.

sergimo
10-29-2004, 05:49 PM
thanx to everyone who answered, i have that working but i also need to be able to go back to the first class when i click on the link again, thanx.

kansel
10-29-2004, 06:47 PM
Replace class1 and class2 with the names of the classes you are using. This toggles between the two on each click.
onclick="this.className=(this.className=='class1')?'class2':'class1';"

sergimo
10-29-2004, 08:53 PM
Thanx to you, now I know more than yestarday but still less than tomorrow



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum