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 Perform: Part 1

Why can't we append the button to the span?

Hey guys, One basic thing that I'm confused about is that why do we hide the span but append to the whole spoiler (<p>) element ? Why couldn't we either hide and append to the span element or hide and append to the whole spoiler(<p>) element?

So, do either

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

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

Or

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

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

2 Answers

If you hide the whole spoiler span, then put the button inside it, wouldn't the button be hidden as well? Same for hiding the whole spoiler element. I think by hiding just the spoiler span, and placing the button outside the span, you're making sure the button displays but the spoiler text doesn't.

Hi Mayank,

I think the reason is because you aren't supposed to add other elements to a span, but I'm not entirely sure. You might want to use the solution below instead.

$(".spoiler span").text("Reveal Spoiler");