3

Why does this line work

$('#body-image').css("background-image", 'url('+ backgroundimage +')');

but not this one

$('#body-image').css("background-image", 'url('backgroundimage')');

or this one

$('#body-image').css("background-image", 'url(backgroundimage)');
3
  • 6
    This is not related to jQuery, but just to basic JavaScript (so jQuery in tag and title is inappropriate). It's in this particular case the string concatenation operator. Commented Sep 15, 2010 at 20:07
  • 5
    Look at the syntax highlighting that SO provides. It's pretty telling. Commented Sep 15, 2010 at 20:10
  • Thanks for all the comments. I think I get it. You have to concatenate the variable into an existing string even though its undefined. Makes sense, and yeah I understand why the last example doesn't work now. Very helpful comments. Commented Sep 15, 2010 at 20:22

5 Answers 5

7

backgroundimage is a JavaScript variable. The concatenation operator in JavaScript is +, so to put a string together with a variable, you do 'some string ' + someVariable. Without the +'s, JavaScript wouldn't know what to do with your variable (and in your third example, wouldn't even know that it was a variable).

Sign up to request clarification or add additional context in comments.

Comments

2

You need to concat the string with the variable backgroundimage. So you use "+" for this.

That's why this doesn't work.

$('#body-image').css("background-image", 'url('backgroundimage')');

And the secont doesn't work because there is no image called 'backgroundimage'.

$('#body-image').css("background-image", 'url(backgroundimage)');

Comments

1

Because you are building a string. You are missing the line where backgroundimage gets a value:

 var backgroundimage = "someimage.gif";
 $('#body-image').css("background-image", 'url('+ backgroundimage +')');  

becomes:

 $('#body-image').css("background-image", 'url(someimage.gif)');  

Comments

0

it's concatenating the string. let's say backgroundimage is 'foo.jpg, then

'url('+backgroundimage+')'  =  'url(foo.jpg)'

Comments

0

In JavaScript, a string literal (i.e., "I am a string") is actually treated like a String object (though, strictly speaking, it isn't - see the MDC documentation - but we can ignore the difference at this level). The following two lines are equivalent:

var letters = "ABC", numbers = "123";
var letters = new String("ABC"), numbers = new String("123");

Strings are concatenated using either the + operator or the String.concat method, either of which join 2 or more strings in a left-to-right order and return the result. So in order to get "ABC123", we can do any of the following:

"ABC" + "123"
"ABC" + numbers
letters + "123"
letters + numbers
"ABC".concat("123")
"ABC".concat(numbers)
letters.concat("123")
letters.concat(numbers)

but not:

letters"123"
"ABC"numbers
lettersnumbers
"lettersnumbers"

which are all, effectively, the same thing that you were trying to do in your examples.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.