Home Tutorials Fonts Lists Freelance Web Designers List Feed RSSContact Me

Thursday, January 31, 2008

Change HTML class attribute using javascript

Save to delicious 0 hits

If you are a JavaScript newbie this post helps you to implement a simple code to change the class for HTML elements.

Some readers asked to me how to change the class attribute of an HTML element using javascript. You can use setAttribute() in this way:


document.getElementById("idElement").setAttribute("class", "className");


...where className is the name of class you want to assign to your html element with ID equal to "idElement".

An example
Image you have a <div> element with a link which call a javascript function changeClass() to change the <div> class:

<div id="myElement">Welcome on my site!</div>
<a href="javascript:changeClass()"&gtChange Class</a>
<script language="javascript">
function changeClass(){
document.getElementById("idElement").setAttribute("class", "myClass");
}
</script>

Sponsored Links
Share this post
Share on Facebook
Share on Twitter
Share on StumbleUpon
Share on Delicious
Share on Reddit
Share on Digg
Share on DesignFloat
Share on Technorati
Feeds RSS Subscribe to Feeds RSS
Old Comments
Eric Wendelin said...

I think that a simpler way may be to use

document.getElementById("blah").className = "mycssclass"

or does this not work with all A-grade browsers?

Antonio said...

Hi Eric, it works on Firefox and Safari on mac. I don't tryied on other browsers :)

EterniCode said...

This works great in FF2, but if you're shooting for cross-browser compatibility (mainly speaking of IE6), save your time and use the element.className ="whatever"; method. It works in all widely-used browsers (to the best of my knowledge).

Using the setAttribute method, IE6 will for some reason apply the classname, but the CSS won't take effect. I really have no clue why...

Max said...

This will not work on any version of Internet Explorer (IE) (Browser Bug 242)
http://webbugtrack.blogspot.com/2007/08/bug-242-setattribute-doesnt-always-work.html

This is actually just ONE of DOZENS of properties that you can not set correctly in IE using the preferred DOM methods.

Ian Cross said...

You can change the class in IE.

Do this:

obj.setAttribute('className', 'TabOn')

Set obj to = your object and
replace TabOn with your class name.

devblog said...

For IE and Mozilla we have two different methods like one is "class" and the other one is "className"

onyeari said...

Hello everyone !
Just wanted to say a big Thank you to Eternicode; Thanks to you, i've been able to get my class css to change dynamically in ie6 using element.className. I tried all ways except this one...and now it works ! Thanks à lot !

Rick Ratayczak said...

I've found that it works like so in IE7 and FF3:

document.getElementById(e).setAttribute("class", c);

document.getElementById(e).setAttribute("className", c);

Rulero said...

Hi.... I was looking for a quick method to update the mask of a textbox, it was so usefull thank you so much.

sciolto said...

weird behaviour in IE6, it jost work if you set both "class" and "className", as rick ratayczak posted

Anonymous said...

Thank you! I'm discovering that backwards compatibility with IE6 is trickier than it sounds.

Anonymous said...

mille grazie. lo uso ora.

Anonymous said...

Thankyou everyone who pointed this bug out and/or supplied a fix. It took me a while to figure out exactly where the problem was since this doesn't throw any errors.

Anonymous said...

Good stuff people. Thanks.

Ulairi said...

And what if I want to add a class, but keeping the classes the element already has?
I mean, I have an element with a class, let's name it myClass1, and
I want to add a secondary class, let's name it myClass2; to apply some effects dynamically.
Is there any method to do it, or do i need to use something like:
document.getElementById("blah").className = document.getElementById("blah").className+" myClass2"

(I don't know if this workaround may work)
thanks in advance!!

Anonymous said...

What i really wanted to change the part inside the >< NOT in the <> example (wanting to change part in italics)
This is a COOL website!
Is there a name for that or something?

About Me

I am Antonio Lupetti, Engineer, Pro Blogger, Mac user, Web addicted. Rome, IT.
Feeds RSS Contact Me

SubscribeFeeds RSS

Contact Me Enter your email address:

SubscribeLatest posts

SubscribeHot Links

Subscribe Design News

Apple Reviews and News
CSSRockstars - You Design, We Code. Your PSD to HTML/CSS for just $149