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

Asia Chan
11,007 PointsSpoiler shows up momentarily before the button shows up. Has anybody had the same problem?
Been following the lesson via workspace, but for some reason, when I refresh the browser, the spoiler momentarily shows up then the button covers it. Is there a way to make the code better to prevent this from happening?
<!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">
<!--Spoiler:-->
<span>Darth Vader is Luke Skywalker's Father! Noooooooooooo!</span>
</p>
<script src="http://code.jquery.com/jquery-3.2.1.min.js" type="text/javascript" charset="utf-8"></script>
<script src="js/app.js" type="text/javascript" charset="utf-8"></script>
</body>
</html>
//Prevent spoilerphobes from seeing spoiler
//Solution: Hide spoilers and reveal them through user interaction
//1. Hide spoiler
$(".spoiler span").hide();
//2. Add a button
$(".spoiler").append("<button>Reveal Spoiler!</button>");
//3. When Button Pressed
$(".spoiler button").click(function() {
// 3.1 Show spoiler
$(".spoiler span").show();
// 3.2 Get rid of button
$(".spoiler button").remove();
});
1 Answer

Steven Parker
243,095 PointsThe trick is to set initial conditions using CSS.
JavaScript runs after the page loads, but CSS becomes effective before the page loads. So to eliminate "flashing", set the initial conditions in the CSS. For example, you could remove this JavaScript line:
//1. Hide spoiler
$(".spoiler span").hide();
And then place this in your CSS file (or in a <style>
tag in the page head) instead:
/* Hide spoiler */
.spoiler span {
display: none;
}

Asia Chan
11,007 PointsHi Steven Parker, thanks! That worked. :)
Ben Payne
1,464 PointsBen Payne
1,464 PointsHey Asia,
Why not add a CSS class like 'hidden' that sets
display: none
to its assigned nodes. Then, using jQuery, toggle that class on and off: