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

Paul Neumyer
Paul Neumyer
15,301 Points

Visual DOM

It would be nice if he visually laid out the DOM when using the .prev() function to select the spoiler preceding the button, even when hiding the elements. I think because it is hidden it throws off your visual hierarchy of the elements, even though in the DOM the structure is still the same.

//PREVENT SPOILERPHOBES FROM SEEING SPOILERS
//SOLUTION: HIDE SPILERS AND REVEAL THEM THROUGH USER INTERACTION

//1, HIDE SPOILER
$(".spoiler span").hide();
//2, ADD A BUTTON
$(".spoiler").append("<button>Reveal Spoiler!</button>");
//3, WHEN BUTTON PRESSED
$("button").click(function () {
  //3.1, SHOW SPOILER NEXT TO THTE BUTTON CLICKED
  $(this).prev().show();
  //3.2, GET RID OF BUTTON
  $(this).remove();

});

1 Answer

Chase Lester
Chase Lester
10,972 Points

Is there a question here?