Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I add a custom string to jQuery .css() function

I'm trying to pass in a custom string to the .css jQuery function. So instead of doing this:

$('#image1').css('background', 'url("img1.png") no-repeat');

I want to do this:

var image = "'url(\"img1.png\") no-reapt'"
$('#image1').css('background', image);

But it's not working. The reason I want to do it like that is because I have 6 elements with 6 different possibilities of different images so I want to add them dynamically. So it would actually look more like this, my variable:

var image = "'url(\"" + this.image.imgs[value[i]] + "\") no-repeat'"
$('#image1').css('background', image);

And it would be inside a loop

like image 930
Van-Sama Avatar asked Dec 08 '25 17:12

Van-Sama


1 Answers

Remove the extra '' symbols. You don't need them. In your string, which is a css style - '' symbols are not valid and they cause not to apply the style.

var image = "url(\"img1.png\") no-repeat";
$('#image1').css('background', image);
like image 171
Suren Srapyan Avatar answered Dec 11 '25 06:12

Suren Srapyan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!