Take the 2-minute tour ×

I was looking at http://docs.jquery.com/Plugins/Authoring#Defaults_and_Options to create a simple plugin for jQuery. Following the section about options and settings, I did the following, which didn't work (the script quit when it encountered the setting).

var settings = {
    'location' : 'top',
    'background-color': 'blue'
$this.css('backgroundColor', settings.background-color); // fails here

Once I removed the dash from the background color, things work properly.

var settings = {
    'location' : 'top',
    'backgroundColor': 'blue' // dash removed here
$this.css('backgroundColor', settings.backgroundColor); 

Am I missing something, or are the jQuery docs wrong?

share|improve this question

4 Answers 4

up vote 36 down vote accepted

no. the parser will interpret it as the subtract operator.

you can do settings['background-color'].

share|improve this answer
I see. Thanks, this is what I should have used. $this.css('backgroundColor', options['background-color']); –  xecaps12 Apr 1 '11 at 16:33
Have to accept this answer because it's right, and includes such witty banter in the comments. –  xecaps12 Apr 1 '11 at 17:06

Change settings.background-color to settings['background-color'].

Variables cannot contain - because that is read as the subtract operator.

share|improve this answer
+1 for because.. –  John Magnolia Feb 23 '12 at 21:21

Dashes are not legal in javascript variables. A variable name must start with a letter, dollar sign or underscore and can be followed by the same or a number.

share|improve this answer

You can have dashes in strings. If you really wanted to keep that dash, you'd have to refer to the property using brackets and whatnot:

$this.css('backgroundColor', settings['background-color']);
share|improve this answer

Your Answer


