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

query structure

Hi guys, about this exercise:

   $(".spoiler span").hide();
   $(".spoiler").append("<button>Reveal Spoiler!</button>");
     $("button").click(function(){
     $(this).prev().show();
     $(this).remove();
     });

in line 4 why we can't do the same as in line 3 ?, like this:

    $("button").prev(function(){
       $("idk what goes in here: prev or this or other").show()
}); 

intead we are allowed to put 2 functions one after the other:

 $(this).prev().show();

thx.

1 Answer

If you look at the jQuery Docs ( http://api.jquery.com/prev/ ) you see that the function .prev() accepts no parameters, meaning that anything placed inside the parentheses will be ignored. All the 'prev' function does is select the previous chlid of that parent, that's why you have to place a 'show' function after so it shows the previous child upon selecting it.

Hopefully this clears up a bit.

Thx very much, it did clear up a lot.