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 jQuery Basics (2014) Creating a Spoiler Revealer Perfect

Andrea Campos
Andrea Campos
4,743 Points

I really don't get this. I don't understand how is it that the .prev() works. Could someone please explain this to me?

I did understand the functionality in the API documentation, the example with the unordered list. I just don't understand it when it is applied to THIS particular example with the buttons. Sorry for my english, though.

4 Answers

Steven Parker
Steven Parker
231,269 Points

It seems well described in the video, but maybe re-phrasing it will help.

It just points to the element BEFORE the one you are using it on. But note that it's specifically for sibling elements, so don't expect it to give you a parent (container) element.

So for example, if you had these two buttons:

  <button id="b1" type="button">first</button>
  <button id="b2" type="button">second</button>

You could identify the first button as:

    $("#b1")         // this is the first button

or as:

    $("#b2").prev()  // this is ALSO the first button

Does that help?

Andrea Campos
Andrea Campos
4,743 Points

I think I get it now. Thanks a lot!

Stanford Kekauoha
Stanford Kekauoha
3,856 Points

Hey Andrea, A little late to the party. Lets see if I can add to the above clarification. It took me a while to get it because in the video he was explaining why use .prev() while in the app.js file and not referencing the html file.

So lets say your page loads and runs your script up to this point...

 //1. Hide Spoiler
 $(".spoiler span").hide();

 //2. Add a button
 $(".spoiler").append( "<button>Reveal Spoiler!</button>" );

After the above script runs, your .spoiler HTML now looks like this:

 <p class="spoiler">
      <!--Spoiler:-->
      <span>Darth Vader is Luke Skywalker's Father! Noooooooooooo!</span>
      <button>Reveal Spoiler</button>
 </p>

 <p class="spoiler">
      <!--Spoiler-->
      <span>Luke and Leia are siblings. Ew.</span>
      <button>Reveal Spoiler</button>
 </p>

Now we want to only show one span at a time when the button is clicked. As in the example, we use the following code:

 $(this).prev().show();
 //Translation: Show the previous sibling of THIS [the .spoiler button]. 
 //What is the previous sibling of the button as seen in the HTML above? The span)

I hope that adds to the what is already here. For me it was hard to see what .prev() was doing by only referencing the app.js file.

Barbara Wiacek
Barbara Wiacek
9,901 Points

Thank you Stanford for this explaination! Now I understand very clearly how it works!

Han Xiao
Han Xiao
10,386 Points

this is really helpful thanks guys!