0
$(document).ready(function() {
    $('#picture').css('height', '666px');
    $('#picture').click(function () {
        $(this).css('background-image', "url('../images/picture_2.jpg')");
    });
});

in the above code, the height is successfully changed to 666px but the background image does not change at all (remains the original one).

When I change the background-image to 'none' instead of "url('../images/picture_2.jpg')" it does make the background image disappear though.

What am I doing wrong?

4
  • try removing single quotes from inside of the url() Commented Jan 9, 2012 at 20:12
  • Which browser? It might just not be loading it.. Commented Jan 9, 2012 at 20:12
  • @FakeRainBrigand chrome and internet explorer 8 Commented Jan 9, 2012 at 20:13
  • possible duplicate question Commented Jan 9, 2012 at 20:13

4 Answers 4

5

My guess is that the URL you're putting in is incorrect. The URL should be relative to the page you're on. I bet your URL is relative to your CSS style sheet. In general, just use absolute paths when doing this in JS.

You also don't need the single quotes surrounding the path.

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

Comments

2

The code you provided should work fine. I think the image location you mentioned ../images/picture_2.jpg is incorrect. Check if you could see the image from that location in firebug.

I tried the same code and it worked fine for me. DEMO here

Comments

1

Correct the path to the image.

Path to the image will be calculated from the root of the site, not from the css.

Just to clarify :

Height will change on the load of the page, Where as Background image will change only if #picture element will be clicked.

Comments

0

Here is a demo, which works for me.

How does this differ from what you've tried?

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.