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

SAHIL SANWAL
SAHIL SANWAL
6,116 Points

styling appended element

is there a way to style the html elements(in this case button) that we append?

2 Answers

Shawn Flanigan
PLUS
Shawn Flanigan
Courses Plus Student 15,815 Points

Sure! Any element that you append to your document will adhere to your stylesheets already in place. Depending on the specific document, there are bound to be a number of ways to select this element, but...giving it a class before appending is one good option. So, in my JavaScript file, I'd do something like:

$(".spoiler").append("<button class='newButton'>Reveal Spoiler</button>");

And then I'd add a CSS rule, like:

.newButton {
    background: #fff;
    color: #333;
}

Hope that makes sense!

Bernadette Guevarra
Bernadette Guevarra
16,295 Points

Yes. If you look at the css stylesheet, the button has already been selected and styled.