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

Dennis Brown
Dennis Brown
28,742 Points

Viewer can see spoiler for a split-second while DOM loading?

When refreshing the page the DOM loads, and you can see the span right before the hide kicks in shortly after. Is this an issue with jQuery 1.x and how Today's browsers load the DOM? I'm using chrome 51.x atm, but surely this is not something browser specific, right?

What sort of steps would be required to make sure that the span cannot be seen by the viewer without moving the script in the HTML? Would this be a case where moving the script would be more beneficial? I'm sure many might say "It's ok, they may not notice", but it seems too sloppy to leave it that way.

2 Answers

Kevin Kenger
Kevin Kenger
32,834 Points

Hey Dennis,

This actually isn't an issue at all. What you said about the location of the script is correct, and that is where it should be. When a page loads, we typically don't want to wait for the JavaScript to load before the rest of the page. That would take too long, and it's not necessary since JavaScript creates the interactivity of our page and we can't interact with it until it's loaded.

I'm assuming that in the video, the spoiler is being hidden with jQuery so that there isn't any interaction with the CSS, and the lesson is purely in jQuery (but I have no idea, I could be totally wrong). That said, in production, you would definitely want to hide the element with CSS and use jQuery to show it to avoid the user seeing the hidden element temporarily.

When I come across showing/hiding elements in real-life scenarios, what I like to do is use a show/hide class in CSS, and toggle that class with JavaScript.

.spoiler span {
    display: none;

.spoiler span.show {
    display: block;
$('button').click(function() {
    $('.spoiler span').toggleClass('show');
Dennis Brown
Dennis Brown
28,742 Points

Ah great!

Yes, the video is using a process of using jQuery purely, and not touching the CSS (yet). I was curious though since the video was not showing this delay, but makes sense why it would being the last thing to load.

Your solution definitely makes much more sense. I'll be sure to keep that in mind, and start linking to CSS classes so to execute earlier in the loading process. Thank you for the explanation and solution!