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

Hiding elements

Hi,

I'm using JS to hide an element using style.display = "none"; but sometimes when I view the page it looks like it display the hidden element for like a nano-second and I'm wondering if there is a way to avoid that?

document.getElementById("user-address").style.display = "none";

2 Answers

Steven Parker
Steven Parker
243,318 Points

:point_right: Use CSS instead of JavaScript to avoid "flashing".

Since JavaScript is applied after the page loads, you can experience a "flash" of a hidden element. But CSS is applied while the page loads, so use that instead:

#user-address {
    display: none;
}

You can still reveal the element using JavaScript later on.

I could use css, but I'm going to use #user-address to reduce the amount of spam mail etc in my contact form and I read somewhere that most spam-bots can't read JS, but I don't know if this is true - that's why I try hide it using JS.