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 Adding New Elements to the DOM

Alvis Chen
Alvis Chen
4,548 Points

Prepend method doesn't work

<!DOCTYPE html> <html> <head> <title>Star Wars Spoilers</title> <link rel="stylesheet" href="css/style.css" type="text/css" media="screen" title="no title" charset="utf-8"> </head> <body> <img src="img/deathstar.png" /> <p class="spoiler"> <span>Darth Vader is Luke Skywalker's Father! Noooooooooooo!</span> </p> <script src="js/jQuery-3.2.1.min.js"></script> <script src="js/app.js"></script> </body> </html>

const $button = $('<button>Reveal Spoiler</button>'); $('.spoiler').prepend($button);

$('.spoiler span').hide(); $('.spoiler button').click(function(){ $('.spoiler span').show(); $('.spoiler button').hide();

2 Answers

Try to format your code in the future but it looks like you are not closing your event handler

const $button = $('<button>Reveal Spoiler</button>'); 
$('.spoiler').prepend($button);

$('.spoiler span').hide(); 

$('.spoiler button').click(function(){ 
    $('.spoiler span').show(); 
    $('.spoiler button').hide();
});
Vasanth Baskaran
Vasanth Baskaran
4,333 Points

Hi Chen,

Try prepending to paragraph ( p ) element.

$('p').prepend($button);

Thanks