Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript jQuery Basics Understanding jQuery Events and DOM Traversal The Event Object

Khaleel Yusuf
Khaleel Yusuf
15,208 Points

I have a question.

How can I make the buttons show different text on different spoilers?

4 Answers

Steven Parker
Steven Parker
217,457 Points

The button text is contained between the tags in the code that creates it. if you identify the spoilers with different selectors you can create different buttons for them:

// Create the normal button
const $button = $('<button>Reveal Spoiler</button>');
// Append only to FIRST spoiler
$('.spoiler:first-of-type').append($button);
//  Create a different button
const $button2 = $('<button>Different Text Here</button>');
// Append only to the OTHER spoiler
$('.spoiler:last-of-type').append($button2);

You could also give unique classes or id's in the HTML and use simpler selectors.

Anthony Currera
Anthony Currera
7,285 Points

I knew it, Jar Jar Binks is Sith!

Khaleel Yusuf
Khaleel Yusuf
15,208 Points

How do I add new buttons?

Steven Parker
Steven Parker
217,457 Points

This same method of creating a button object and then using the 'append" method to place it on the page can be used to add other buttons as you wish. But it's probably more common just to write them into the HTML.

Khaleel Yusuf
Khaleel Yusuf
15,208 Points

When you selected the spoiler, you either wrote first-of-type or last-of-type. If I have three spoilers, what do I call the second spoiler?

Steven Parker
Steven Parker
217,457 Points

.spoiler:nth-of-type(2)