0

Motto is to set/update(ajaxified) a DIV background image.

        $.getJSON("https://itunes.apple.com/search?term=" + searchTerm + '&limit=1' + '&callback=?', 
        function(data) {
        $.each(data.results, function() {
        var art = this.artworkUrl100;
        $('.photo').parent().css('background-image', 'url(' + art + ')');
      }
    }

On HTML i've this:

<div id="results" class="photo"></div>
2
  • 1
    What is the parent div that you are setting the background image on? Or are you wanting to set the background on the .photo div? Commented Apr 7, 2016 at 23:14
  • want to set background image on div class 'photo' Commented Apr 7, 2016 at 23:16

1 Answer 1

2

You are setting the background image to a parent element instead of the .photo div.

Try this:

$.getJSON("https://itunes.apple.com/search?term=" + searchTerm + '&limit=1' + '&callback=?', 
    function(data) {
        $.each(data.results, function() {
            var art = this.artworkUrl100;
            $('.photo').css('background-image', 'url(' + art + ')');
        })
});

If multiple data.results are returned then only the last be used for the background image due to the $.each loop that you are using. If you are maintaining the limit=1 on the JSON call then this won't be a problem.

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

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.