Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Applying a lightbox to images from API

I'm trying to create a lightbox. I've done it before but not with using JSON data for the images. Please help me figure out what I'm doing wrong /missing. I've included my HTML, CSS, and JavaScript. In each set of code, I've commented where the lightbox code is. Thanks in advance for your help.

HTML

<!DOCTYPE html>

  <html>

    <head>

      <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>

      <script src="script.js"></script>

      <link rel="stylesheet" type="text/css" href="styles.css">

      <link rel="stylesheet"

 href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">

    </head>

    <body>

      <div class="top">

        <h1>Spotify Song Search</h1>

        <form id="search-form">

          <input type="search" placeholder="Search for a album or band" id="song">

          <button type="submit" id="search">Search</button>

        </form>  

      </div>

      <div id="music"> <!-- images with lightbox should go here -->

        </div>

      </div>

    </body>

  </html>

CSS

body {
background-color:

}

.top {
text-align: center;

}

#music {
margin-left: 40px;
margin-right: 40px;
margin-top: 40px;

}

.album {
display: inline-block;

}

li {
list-style: none;
padding: 10px;
}

img {
width: 200px;

}

input {
width: 400px;
height: 30px;
}

// overlay for lightbox

#overlay {

background-color: rgba(0,0,0, 0.7);

width: 100%;

height: 100%;

position: absolute;

top: 0;

left:0;

display: none;

text-align: center;

}

#overlay img {
margin-top: 10%;

height: 360px;

width: 360px;

}

#overlay p {

color: white;


}

JavaScript

$(document).ready(function() {

    $('form').submit(function(evt) {

        evt.preventDefault();

        // var $searchField = $('#song');

        var spotifyAPI = "https://api.spotify.com/v1/search" // needs end point

        // var albums = $searchField.val();// Specifies type of user info to be retrieved and sent to Spotify API

        var spotifyOptions = {

            "type": "album",

            "query": $("#song").val(),

            "limit": "12"


        }; // Requests data from spotify API. Must be a JavaScript Object
        $.getJSON(spotifyAPI, spotifyOptions, displayAlbums); // Currently, these are only placeholders. The variables inserted as arguments have not been defined as variables
        function displayAlbums(data){
            var albumHTML = '<ul>'; // Opening ul tag to hold a single album result
            $.each(data.albums.items, function(i, album){
                albumHTML += '<div class="album col-md-3" album-id="' + album.index + '">';
                albumHTML += '<li>'; // You may need to use bootstrap to style array items correctly
                albumHTML += '<a href="#">';
                albumHTML += '<img src="' + album.images[0].url + '" alt="' + album.artists.name + '">';
                albumHTML += '</a>';
                albumHTML += '</li>';
                albumHTML += '</div>';
                return albumHTML;
            });

                albumHTML += '</ul>';
                $('#music').html(albumHTML);

            }; 

// supposed to be the lightbox

            var $overlay = $('<div id="overlay"></div>');
            var $image = $("<img>");
        var $caption = $("<p></p>");            

            $overlay.append($image);
            $overlay.append($caption);

            $("body").append($overlay);

            $("#song a").click(function(event){
                event.preventDefault();
                var imageLocation = $(this).attr("src"); // May need to specify a second argument
                $image.attr("src", imageLocation);
                $overlay.show();

                var captionText = $(this).children(img).attr("alt");
                $caption.text(captionText);
            }); 

            $overlay.click(function(){
                $overlay.hide();

            });

        });

    });

1 Answer

I'd start by just adding console.log statements wherever you're using inputs and expecting outputs.

For example, add the following as the first line inside your displayAlbums function:

console.log(data);

That way you'll have a better idea of the structure of the data.

Also try adding an error handler so you know if something is wrong with the API call