views:

113

answers:

4

I have some JQuery code that shows or hides a div.

$("div#extraControls").show();   // OR .hide()

I initially want the div to be not visible so I used:

$(document).ready(function() {
    $("div#extraControls").hide();
});

However, on the browser, the content loads visible for a second before disappearing, which is not what I want.

How do I set the hide the element before the page loads whilst keeping the ability to show hide it dynamically with a script?

+2  A: 

In CSS:

#extraControls { display: none; }

Or in HTML:

<div id="extraControls" style="display: none;"></div>
Caleb
+5  A: 
div.hidden
{
   display: none
}

<div id="extraControls" class="hidden">
</div>

$(document).ready(function() {
    $("div#extraControls").removeClass("hidden");
});
Developer Art
+1 One thing to note is that if the user has disabled JavaScript in the browser, the whole page stays hidden.
Kimmo Puputti
From my experience some browsers will not load media (eg. images) in elements which are hidden using `display:none` or `opacity:0`. I'd recommend using `visibility:hidden` instead.
mqchen
@mq.chen Thanks for the warning, however the visibility tag does not allow the content to be toggled with the .hide() command.
Robert
A: 

What you could do is insert a inline script tag at the end of the document, or immediately after the div with id "extraControls". Should be fire a little sooner than.

<div id="extraControls">i want to be invisible!</div>
<script type="text/javascript">$("div#extraControls").hide()</script>

Of course you could do this server-side as well, but maybe there's a good reason you don't want to do that (like, have the controls visible if the browser does NOT support javascript).

CharlesLeaf
+1  A: 

Make a css class

.hidden {
display: none;
}

Add this to any element you don't want to be visible when loading the page. Then use $("div#extraControls").show(); to display it again.