Javascript Show/Hide -- It Works except it makes me click twice to SHOW the first time

tonyhhisc used Ask the Experts™
I have included the code for a simple show/hide link. Which will either show or hide the specified div depending on it's current state. It works, all except for you have to click it TWICE to open it the first time. After that, one single click opens or hides as expected.

Anyone have an Idea why?? Thanks for your help! Please copy/paste/test the code and you'll see exactly what I'm talking about.
li a { font-size: 8pt; }
#folder_1 { display: none; }
#folder_2 { display: none; }
#folder_3 { display: none; }
.folderCollapsed { display: none; }
<script type="text/javascript" language="JavaScript"><!--
function ReverseContentDisplay(d) {
if(d.length < 1) { return; }
if(document.getElementById(d).style.display == "none") { document.getElementById(d).style.display = "block"; }
else { document.getElementById(d).style.display = "none"; }
<a href="javascript:ReverseContentDisplay('folder_1')">FOLDER #1</a><br />
<div class="folderCollapsed" id="folder_1">
folder one contents
<br />
<a href="javascript:ReverseContentDisplay('folder_2')">FOLDER #2</a><br />
<div class="folderCollapsed" id="folder_2">
folder two contents
<br />
<a href="javascript:ReverseContentDisplay('folder_3')">FOLDER #3</a><br />
<div class="folderCollapsed" id="folder_3">
folder three contents

Open in new window

Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

How about if you used visibility: hidden?
just swap them around.

In the DOM, the first time around will be null or ''.
if(document.getElementById(d).style.display == "block") { document.getElementById(d).style.display = "none"; }
else { document.getElementById(d).style.display = "block"; }

Open in new window


thank you thank you thank you thank you thank you thank you thank you

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial