Jump to content

How to change a style property with javascript and DOM


Lepper

Recommended Posts

I use the following but it doesn't work

document.getElementById("myThing").style.background-color = "yellow";

However, this style property does work:

document.getElementById("myThing").style.display = "block";

Am I accessing wrong the property background-color?Thanks!

Link to comment
Share on other sites

just to explain why.Javascript doesn't allow (-) in member names ( a member is a property or function of an object) so you need to use camleCase.You will run into a problem with the float property aswell so you will need to do something like the following.

//float is a keyword in other browsers besides IE so you have to account for thatif(navigator.appName == "Microsoft Internet Explorer")  document.getElementById('myThing').style.float = 'left';else  document.getElementById('myThing').style.cssFloat = 'left';

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...