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 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