Scott Hunter
Scott Hunter

Reputation: 415

Variable inside a jquery background change

I'm having troubles with the syntax for a simple line of jquery, I cant seem to insert the 'this.src' part correctly, I get muddled up with quotes and commas etc. Heres what I currently have :

$('#blog-hover-image').css("background-image", "url(", this.src, ")");

How do I insert the this.src part correctly please ?

I have a url stored in this.src from earlier in the file.

Thank you.

Upvotes: 0

Views: 28

Answers (2)

user9263373
user9263373

Reputation: 1074

You were close but you need to use the + for string concatenation, not the ,.

$('#blog-hover-image').css("background-image", "url('" + this.src + "')");

Upvotes: 1

j08691
j08691

Reputation: 208002

Like this

$('#blog-hover-image').css("background-image", "url(\"" + this.src + "\")");

Upvotes: 3

Related Questions