1

I'm trying to add a dynamic background using the .css() property of jQuery. The background URL is provided through AJAX:

            function requestbg(cusa) {
                $.ajax({
                    url: 'api/bg.php',
                    type: 'GET',
                    data: {id: cusa},
                    success: function(response) {
                        if(response != "") {
                            var url = response;
                            $("body").css("background-url", url);
                            console.log(response);
                        }
                    }
                });
            }

When changing background-url to background-color, it does work. Weird, because in the console I log the response, and it does return a URL. Figured there might be a space somewhere, checked it multiple times but nothing.

Does anybody know why this would not work?

1
  • 1
    In CSS there is no such property "background-url". Explain what you mean by "background-url". Commented Jun 9, 2018 at 21:51

2 Answers 2

1

background-url is an invalid css property. You're looking for background-image:

The background-image CSS property sets one or more background images on an element.

Adjust your code as so and the follow should work.

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

1 Comment

I've got it working. Thanks for the help and explanation, it's appreciated :)
0

Try this:

$("body").css({ "background-image": 'url('+url+')'});

2 Comments

That indeed seems to work perfectly. Just for future reference, is there any particular reason why my code wouldn't work and this does?
Sorry about that , currently on my phone answering via app. In short, 'background-url' is not a case property. 'background-image' is what you should be using, supplying a value matching the required format 'url(your_image_url)'. Hope this clarifies my answer a bit

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.