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 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
231,275 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
231,275 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
231,275 Points

.spoiler:nth-of-type(2)