Jump to content

Can I set an Element's Properties via CSS class


watagal

Recommended Posts

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

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

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

Excellent explanation, I couldn't do any better :)

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 lol
Please 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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...