2

In my script I have variable image_src like

var image_src = 'img/test.jpg';

then I tried to add background-image to block using jQuery

$('#lightbox').css({
        'background-image': '(../' + image_src + ')'
    });

but it doesn't work. What's wrong ?

truslivii.lev
  • 701
  • 1
  • 8
  • 21
  • Possible duplicate: http://stackoverflow.com/questions/512054/setting-background-image-using-jquery-css-property – effone Mar 27 '14 at 08:27

3 Answers3

1

You are missing url in set value.try this:

 $('#lightbox').css({
    'background-image': 'url(../' + image_src + ')'
});
Milind Anantwar
  • 81,290
  • 25
  • 94
  • 125
1

You need to set your CSS property with a URL datatype:

$('#lightbox').css({
   'background-image': 'url(../' + image_src + ')'
});
CodingIntrigue
  • 75,930
  • 30
  • 170
  • 176
1

What error message are you getting?

This should work:

$('#lightbox').css({
   'background-image': 'url(../' + image_src + ')'
});
martynas
  • 12,120
  • 3
  • 55
  • 60