Register  |  Login




Advertisement

Start Your Own Q&A Site

Create your own Q&A site easily, allowing you to quickly grow a new community around any subject matter or generate new organic traffic for your existing website.

Question

Status: Closed Points: 75 Time: 18:33 - Sep 13, 2006  

jgivoni

How do I change the style of an element in javascript, when the style attribute has a hyphen?

For example, imagine I have an element like this:
<div id="test" style="width: 100px; border-top: 1px solid black"/>

Normally I can change the style dynamically like this:
document.getElementById("test").style.width = "200px";

But, similarly, this doesn't work:
document.getElementById("test").style.border-top = "2px solid green";

Apparantly, the hyphen (-) in the style attribute (border-top) is causing trouble (being interpreted as a minus).
So what is the correct syntax in this case?

Thanks,
Jakob

Answer Discussion
Tutorials

 

multani.sarbjit

Date:: Sep 14, 2006

Time:: 07:18

Hi Jakob,

Try this
document.getElementById("test").style: border-top: 2px solid green;

Regards,
Sarbjit

Anpanman

Date:: Sep 14, 2006

Time:: 09:18

Hi jakob,
"borderTop" is the right way to refer to that property

document.getElementById("test").style.borderTop = "2px solid green";

/Anpanman

jgivoni

Date:: Sep 14, 2006

Time:: 14:04

Anpanman, you're right. Quite simple!

rcastagna

Date:: Sep 14, 2006

Time:: 14:11

Jakob,

As Anpanman points out, dropping the hyphen in border-top will give you the desired results. This is carried over for all of the hyphenated styles:
background-color = backgroundColor
font-weight = fontWeight
margin-top = marginTop
etc.

I've also found that case is important to some of the browsers with strict interpretation, so make sure the second "word" in the attribute is capitalized.

Something to look at that really helps with javascript (as well as other things HTML) is the Visibone charts found at http://www.visibone.com. I've got the "Card Collection" book, and there isn't a day that I don't look at it to find something I'm trying to resolve.

I hope this helps, too...

Question Answered

This question has been closed, and points have been rewarded to the following experts:


Anpanman: 75

You're welcome however to comment or give additional information or if you wish, you have the ability to write a Tutorial in the Tutorial Area.

Answer this Question

New User

Email:

Upon submission of this form, you will automatically be registered as a Quomon user and we will send your login information to this address

Registered User

Username:

Password:

Forgot Your Password?

jgivoni

Date:: Sep 14, 2006

Time:: 14:06

Skip the hyphen and capitalize the next letter; i.e.:

element.style.borderTop = "2px solid green"


Click here to see the Answer Discussion that preceded this tutorial.


Login to rate this tutorial: Good  |  Bad

Answer this Question

New User

Email:

Upon submission of this form, you will automatically be registered as a Quomon user and we will send your login information to this address

Registered User

Username:

Password:

Forgot Your Password?

Ask a Question

Have a new question? Ask!

You have 100 characters to use



Top Experts

View More

Rank

Expert

Points

1.

nidhi

10354

2.

oracleofDelphi

6493

3.

rcastagna

5596

4.

LAGM

4848

5.

PeterNZ

3487

6.

gonzalo

2840

7.

Mason

2770

8.

jgivoni

2303

9.

xarcus

1820

10.

Anpanman

917

Become an Expert

Register today to share your knowledge with the community and be recognized and rewarded for your contributions.


Register Here




"Psst, Quomon is a great site. Pass it on."     Tell a Friend  |   Link To Us  |   Save to Delicious  |   Digg! Digg it



Language Options

English:

www.quomon.com

Español:

www.quomon.es