1

Is there a reason why I can do this:

document.getElementById('gridID').style.background='blue';

but when I try this:

document.getElementById('gridID').style.background-color='blue';

throws this:

 Uncaught ReferenceError: Invalid left-hand side in assignment

I need to access backgroundPosition and some other things and they just aren't working. They do not appear in xcode's autosuggest when I type and they do not work when I try them anyways. Any thoughts? I haven't found similar problems on Google to my surprise.

Thanks, Chet.

1
  • It may be worth noting that gridID is the id for a div. Commented Mar 19, 2012 at 3:31

2 Answers 2

4

Javascript identifiers cannot contain -s.

Instead, use camelCase:

document.getElementById('gridID').style.backgroundColor='blue';
Sign up to request clarification or add additional context in comments.

2 Comments

Not true, actually - myObject['background-color'] is perfectly valid. But you can't use - in dot notation, because it is interpreted as a minus sign.
@nrabinowitz: True, although that won't actually do anything. Clarified.
1

try:

document.getElementById('gridID').style.backgroundColor='blue';

for styles that have more than one word (like background-color) JS uses camelCased names instead of dash-separated names,

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.