8

I am trying to change the background image of an element through jquery like this;

$j(this).css('background-image','url(images/client_box_grad.gif)');

However when it renders it seems to drop speech marks around the the url, eg

$j(this).css('background-image','url("images/client_box_grad.gif")');

And this means the image isn't visible - if I remove the speech marks in Firebug then the image shows up.

Any ideas ?

3 Answers 3

11

The double quotes are not necessary:

$(this).css('background-image', 'url(/images/client_box_grad.gif)');

You should make sure that you have specified a valid image url. Here's a demo.

Sign up to request clarification or add additional context in comments.

1 Comment

Cheers Darin - it works when I stick the absolute path on, I'd like to use relative pathing if possible, as it's sat in a wordpress install where everything has been made relative so nothing broke when I stuck it live from my dev area. I can live with that if I can't use any form of relative pathing though. Also it seems to add in the double quotes as part of the replacement - they are not there in any of the code.
6

This is what I had to do to get it to work:

$(function(){
$('body').css({backgroundImage : 'url(/media/bill.jpg)'});
});

1 Comment

I have tried all other ways that are mentioned in other answers. No one worked form me. But your solution is worked like a charm for me.
2

Try these:

$(this).css('background-image','url(images/client_box_grad.gif)');

// OR
$(this).css('background', 'url("images/client_box_grad.gif")');

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.