watagal Posted May 16, 2006 Share Posted May 16, 2006 GreetingsI want to do something like:document.getElementById('id').class = "myCSSclass";ordocument.getElementById('id').style.class = "myCSSclass";I realize the above code doesn't work, but is there a way to do it?WG Link to comment Share on other sites More sharing options...
skym Posted May 16, 2006 Share Posted May 16, 2006 It's called 'className'. I found it athttp://www.javascriptkit.com/domref/elementproperties.shtmlWorks in IE/FF/Opera: <html><head><style type="text/css">.class_one { color: #0000FF; font-weight: normal; }.class_two { color: #FF0000; font-weight: bold; }</style><script type="text/javascript"><!--function changeClassOne() { document.getElementById('myDiv').className = 'class_one'; }function changeClassTwo() { document.getElementById('myDiv').className = 'class_two'; }//--></script></head><body><div id="myDiv">HELLO</div><br><input type="button" value="CLASS ONE" onClick="changeClassOne()"><input type="button" value="CLASS TWO" onClick="changeClassTwo()"></body></html> Link to comment Share on other sites More sharing options...
Jack McKalling Posted May 16, 2006 Share Posted May 16, 2006 Excellent explanation, I couldn't do any better Link to comment Share on other sites More sharing options...
Little Goat Posted May 16, 2006 Share Posted May 16, 2006 hey, thanx skym, I couldn't get something to work the other day because of this. LG Link to comment Share on other sites More sharing options...
Jack McKalling Posted May 16, 2006 Share Posted May 16, 2006 Classes are actually "className" in DOM (Document Object Model ), which is what you are using, and it is one of some that are different from what would be logical.To get a div object: getElementsByTagNameSo TagName instead of Tag, it returns an array so elements in plural. Maybe there are more like this, but nevermind lol Link to comment Share on other sites More sharing options...
watagal Posted May 17, 2006 Author Share Posted May 17, 2006 Thanks Skym, what a great reference! Link to comment Share on other sites More sharing options...
aspnetguy Posted May 17, 2006 Share Posted May 17, 2006 Excellent explanation, I couldn't do any better <{POST_SNAPBACK}> but you tried anyway Classes are actually "className" in DOM (Document Object Model happy.gif), which is what you are using, and it is one of some that are different from what would be logical.To get a div object: getElementsByTagNameSo TagName instead of Tag, it returns an array so elements in plural. Maybe there are more like this, but nevermind lolPlease don't just copy what someone else is saying...there is no value in these kind of posts. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now