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 ?

1

3 Answers 3

1

You are missing url in set value.try this:

 $('#lightbox').css({
    'background-image': 'url(../' + image_src + ')'
});
Sign up to request clarification or add additional context in comments.

Comments

1

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

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

Comments

1

What error message are you getting?

This should work:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.