John John - 1 year ago 135
HTML Question

How to start off my jquery toggles as hidden?

I have some code which displays three images and a respective div under each image. Using the jquery .toggle function I have made it so each div will toggle when the image above it is clicked. Is there any way to make it so the divs start off as hidden?

Thank you for your time,


<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">






<h2>Smooth Transitions</h2>

<img src="one.png" id="picOne">
<div id="one">
<p>first paragraph.</p>

<img src="two.png" id="picTwo">

<div id="two" visibility="hidden">
<p>Second Pair of giraffe.</p>

<img src="three.png" id="picThree">

<div id="three">
<p>Thiird paragraph.</p>


Answer Source

visibility is a css property. But display is what you want in any event

<div id="two" style="display:none;">
  <p>Second Pair of giraffe.</p>

Or ditch the inline css and give each div to be toggled a css class

<div id="two" class="initiallyHidden">

and then

.initiallyHidden { display: none; }

And you can also clean up your jQuery a bit. Give your toggle-causing images a css class

    <img src="one.png" class="toggler">
    <p>first paragraph.</p>

And then


This would obviate your need for all those IDs, and would also make this solution much, much more extensible.

And to handle dynamically added content, you could use on instead of click

$(document).on("click", "img.toggler", function(){

(and for better performance, make sure all these toggling images are in some container div, named, say, containerFoo, then do $("#containerFoo").on instead of $(document).on(

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download