I have a URL in a variable, and I am having issues setting the value in CSS.
$('myObject').css('background-image', imageUrl);
// $('myObject').css('background-image'); returns 'none'
Any pointers?
You probably want this (to make it like a normal CSS background-image declaration):
| |||||||||||||||||
|
You'll want to include double quotes (") before and after the imageUrl like this:
This way, if the image has spaces it will still be set as a property. | |||||||||||||||||
|
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:
| |||||||||
|
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:
| |||||
|
For those using an actual url and not a variable:
| |||
|