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

Jess Hines
Jess Hines
5,411 Points

Spoiler! Shorter code.

I can't resist perfecting when I can, and since I was already looking at the jQuery API documentation, I noticed the children() and toggle() functions, and came up with this. Any problems with doing it this way?

$('.spoiler span').hide();
$('.spoiler').append('<button>Reveal spoiler!</button>');
//The span with the spoiler text is now hidden, and the button is visible
//When any of the .spoiler paragraphs are clicked, toggle the visibility
//of that paragraph's children so what's visible is hidden and what was hidden becomes visible.
$('.spoiler').click(function() {
  $(this).children().toggle();
});

1 Answer

Young Bae
Young Bae
18,429 Points

haha the button is also showed up again when the spoiler is clicked. I like it! :)