Applying a lightbox to JSON Data

Applying a lightbox to JSON Data

I'm trying to pull images from the spotify API and give those images a lightbox. I've created a lightbox for images before but just not with using JSON data. Not sure what my code is missing. Your help would be greatly appreciated.

This is an example of the JSON data that I'm trying to pull from. Below, I've bolded the areas in my HTML, CSS, and Javascript which are directly related to the lightbox.

HTML

  1. <!DOCTYPE html>
  2.   <html>
  3.     <head>
  4.       <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
  5.       <script src="script.js"></script>
  6.       <link rel="stylesheet" type="text/css" href="styles.css">
  7.       <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  8.     </head>
  9.     <body>
  10.       <div class="top">
  11.         <h1>Spotify Song Search</h1>
  12.         <form id="search-form">
  13.           <input type="search" placeholder="Search for a album or band" id="song">
  14.           <button type="submit" id="search">Search</button>
  15.         </form>  
  16.       </div>
  17.       <div id="music"> <!-- images with lightbox should go here -->
  18.         </div>
  19.       </div>
  20.     </body>

  21.   </html>

CSS

  1. body {
  2. background-color:

  3. }

  4. .top {
  5. text-align: center;

  6. }

  7. #music {
  8. margin-left: 40px;
  9. margin-right: 40px;
  10. margin-top: 40px;

  11. }

  12. .album {
  13. display: inline-block;

  14. }

  15. li {
  16. list-style: none;
  17. padding: 10px;
  18. }

  19. img {
  20. width: 200px;

  21. }

  22. input {
  23. width: 400px;
  24. height: 30px;
  25. }

  26. #overlay {
  27. background-color: rgba(0,0,0, 0.7);
  28. width: 100%;
  29. height: 100%;
  30. position: absolute;
  31. top: 0;
  32. left:0;
  33. display: none;
  34. text-align: center;
  35. }

  36. #overlay img {
  37. margin-top: 10%;
  38. height: 360px;
  39. width: 360px;
  40. }

  41. #overlay p {
  42. color: white;

  43. }
JavaScript

  1. $(document).ready(function() {
  2. $('form').submit(function(evt) {
  3. evt.preventDefault();
  4. // var $searchField = $('#song');
  5. var spotifyAPI = "https://api.spotify.com/v1/search" // needs end point
  6. // var albums = $searchField.val();// Specifies type of user info to be retrieved and sent to Spotify API
  7. var spotifyOptions = {
  8. "type": "album",
  9. "query": $("#song").val(),
  10. "limit": "12"

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

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

  28. };
  29. var $overlay = $('<div id="overlay"></div>');
  30. var $image = $("<img>");
  31. var $caption = $("<p></p>");

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

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

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

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

  43. $overlay.click(function(){
  44. $overlay.hide();
  45. });

  46. });

  47. });