Register  |  Login



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
Answer Summaries

 

Q&A System for Websites and Corporate Collaboration

Advertisement

  • Generates significant organic traffic for websites
  • Saves companies money, resources, and time

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 an Answer Summary for the Summary 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 summary.


Login to rate this summary: 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 information technology Experts

View More

Rank

Expert

Points

1.

oracleofDelphi

520

2.

nidhi

510

3.

rcastagna

495

4.

PeterNZ

475

5.

theDude

250

6.

Anpanman

150

7.

dustPuppy

100

8.

multani.sarbjit

75

9.

bit2bit

60

10.

rberbe2002

60

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