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 Adding jQuery

Ilya Chekharin
Ilya Chekharin
6,341 Points


$('button').removeClass("selected"); -- Why does this work? $(this).removeClass("selected");--And this doesn't?

    var flickerAPI = "http://api.flickr.com/services/feeds/photos_public.gne?jsoncallback=?";
    var animal = $(this).text();
    var flickrOptions = {
      tags: animal,
      format: "json"
    function displayPhotos(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>';
    photoHTML += '</ul>';
    photoHTML += '</ul>';
    $.getJSON(flickerAPI, flickrOptions, displayPhotos);
}); // end ready
<!DOCTYPE html>
  <meta charset="utf-8">
  <title>AJAX Flickr Photo Feeder</title>
  <link href='http://fonts.googleapis.com/css?family=Varela+Round' rel='stylesheet' type='text/css'>
  <link rel="stylesheet" href="css/main.css">
  <script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
  <script src="js/flickr.js"></script>
  <div class="grid-container centered">
    <div class="grid-100">
      <div class="contained">
        <div class="grid-100">
          <div class="heading">
            <h1>Flickr Photo Feeder</h1>
            <p>Choose which kind of animal you'd like to return photos of...</p>
          <ul class="filter-select">

        <div id="photos">

Seth Kroger
Seth Kroger
56,412 Points

I'd need to see more of your code to give you a proper answer but it could be either 1) the handler is attached to something other than the button so 'this' refers to something else in this context. 2) you're using an arrow function and arrow functions don't have their own 'this' like a regular function would.

3 Answers

Because "$(this)" refers to the button the user is clicking, which means when the user clicks to remove the "selected" class it would just loop and add the "selected" class again. This is why he selected all the other "buttons" "$('button').removeClass("selected");" to have their class removed when the user clicks another button.

I just came across this question as I was doing the video. Sorry for the late response. Hope it helped.

Ilya Chekharin
Ilya Chekharin
6,341 Points

Sorry for the late reply, I added the full code above.

I need to see the full code but I'm sure the context of 'this' is not button, you need to understand the concept of scope and how it works on your function.