truslivii.lev
truslivii.lev

Reputation: 701

Add background-image using jQuery

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 ?

Upvotes: 2

Views: 86

Answers (3)

martynas
martynas

Reputation: 12300

What error message are you getting?

This should work:

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

Upvotes: 1

CodingIntrigue
CodingIntrigue

Reputation: 78525

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

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

Upvotes: 1

Milind Anantwar
Milind Anantwar

Reputation: 82231

You are missing url in set value.try this:

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

Upvotes: 1

Related Questions