Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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,407 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.