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 AJAX Basics (retiring) AJAX and APIs Displaying the Photos

Button does not stay clicked and photos do not show

Compared line by line multiple times and may have missed something.

$(document).ready(function() {

// user clicks button

  $("button").click(function() {
    $("button").removeClass("selected");
    $(this).addClass("selected");

    // JS obtains word on the button
    var flickerAPI = "http://api.flickr.com/services/feeds/photos_public.gne?jsoncallback=?";
    var animal = $(this).text();
    var flickerOptions = {
      tags: animal,
      format: "json"
    };

    // add a link and <img> to the page, receive JSON response
    var displayPhotos = function(data) {
      var photoHTML = '<ul>';
      $.each(data.items, function(i, photo) {
        photoHTML += '<li class="grid-25 tablet-grid-50">';
        photoHTML += '<a href="' + photo.link + '" class="image">';
        photoHTML += '<img src="' + photo.media.m + '"></a></li>';
      }); // end loop

      photoHTML += '</ul>';
      $('#photos').html(photoHTML);
    } // end function displayPhotos

    // make a GET request to Flickr, sending the word

    $.getJSON(flickerAPI, flickerOptions, displayPhotos);

  }); // end button click

}); // end ready

2 Answers

Robert Leonardi
Robert Leonardi
17,151 Points

Hey Grace,

Why do you want to have the button stay clicked? What are you trying to achieve?

I've tried your code and it runs great. The pics are showing. I add "$('#photos').empty();" before displaying the pics, just so i can always have the updated pics whenever I clicked the button.

Best of luck

Caitrin Roesler
Caitrin Roesler
7,671 Points

I am having the same issues as Grace. This answer above did not help....