Setting background-image using jQuery CSS property

JavascriptJqueryCss

Javascript Problem Overview


I have an image URL in a imageUrl variable and I am trying to set it as CSS style, using jQuery:

$('myObject').css('background-image', imageUrl);

This seems to be not working, as:

console.log($('myObject').css('background-image'));

returns none.

Any idea, what I am doing wrong?

Javascript Solutions


Solution 1 - Javascript

You probably want this (to make it like a normal CSS background-image declaration):

$('myObject').css('background-image', 'url(' + imageUrl + ')');

Solution 2 - Javascript

You'll want to include double quotes (") before and after the imageUrl like this:

$('myOjbect').css('background-image', 'url("' + imageUrl + '")');

This way, if the image has spaces it will still be set as a property.

Solution 3 - Javascript

Alternatively to what the others are correctly suggesting, I find it easier usually to toggle CSS classes, instead of individual CSS settings (especially background image URLs). For example:

// in CSS 
.bg1 
{
  background-image: url(/some/image/url/here.jpg);
}

.bg2 
{
  background-image: url(/another/image/url/there.jpg);
}

// in JS
// based on value of imageUrl, determine what class to remove and what class to add.
$('myOjbect').removeClass('bg1').addClass('bg2');

Solution 4 - Javascript

Here is my code:

$('body').css('background-image', 'url("/apo/1.jpg")');

Enjoy, friend

Solution 5 - Javascript

Further to the other answers, you can also use "background". This is particularly useful when you want to set other properties relating to the way the image is used by the background, such as:

$("myObject").css("background", "transparent url('"+imageURL+"') no-repeat right top");

Solution 6 - Javascript

The problem I was having, is that I kept adding a semi-colon ; at the end of the url() value, which prevented the code from working.

NOT WORKING CODE:

$('#image_element').css('background-image', 'url(http://example.com/img.jpg)<strong>;</strong>';);
//--------------------------------------------------------------------------^

WORKING CODE:

$('#image_element').css('background-image', 'url(http://example.com/img.jpg)');

Notice the omitted semi-colon ; at the end in the working code. I simply didn't know the correct syntax, and it's really hard to notice it. Hopefully this helps someone else in the same boat.

Solution 7 - Javascript

For those using an actual URL and not a variable:

$('myObject').css('background-image', 'url(../../example/url.html)');

Solution 8 - Javascript

Try modifying the "style" attribute:

$('myObject').attr('style', 'background-image: url("' + imageUrl +'")');

Solution 9 - Javascript

String interpolation to the rescue.

let imageUrl = 'imageurl.png';
$('myOjbect').css('background-image', `url(${imageUrl})`);

Solution 10 - Javascript

Don't forget that the jQuery css function allows objects to be passed which allows you to set multiple items at the same time. The answered code would then look like this:

$(this).css({'background-image':'url(' + imageUrl + ')'})

Solution 11 - Javascript

$('myObject').css({'background-image': 'url(imgUrl)',});

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBlankmanView Question on Stackoverflow
Solution 1 - JavascriptGregView Answer on Stackoverflow
Solution 2 - JavascriptArosboroView Answer on Stackoverflow
Solution 3 - JavascriptKonView Answer on Stackoverflow
Solution 4 - JavascriptApoView Answer on Stackoverflow
Solution 5 - JavascriptMatt ParkinsView Answer on Stackoverflow
Solution 6 - JavascriptPridView Answer on Stackoverflow
Solution 7 - JavascriptDustyView Answer on Stackoverflow
Solution 8 - JavascriptappcropolisView Answer on Stackoverflow
Solution 9 - JavascriptSushanth --View Answer on Stackoverflow
Solution 10 - JavascriptAntonyView Answer on Stackoverflow
Solution 11 - JavascriptwahajView Answer on Stackoverflow