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

tonyhhisc
tonyhhisc used Ask the Experts™
on
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.
<html>
<head>
<style>
li a { font-size: 8pt; }
#folder_1 { display: none; }
#folder_2 { display: none; }
#folder_3 { display: none; }
.folderCollapsed { display: none; }
</style>
 
<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"; }
}
//--></script>
</head>
 
<body>
 
 
<a href="javascript:ReverseContentDisplay('folder_1')">FOLDER #1</a><br />
<div class="folderCollapsed" id="folder_1">
folder one contents
</div>
 
<br />
 
<a href="javascript:ReverseContentDisplay('folder_2')">FOLDER #2</a><br />
<div class="folderCollapsed" id="folder_2">
folder two contents
</div>
 
<br />
 
<a href="javascript:ReverseContentDisplay('folder_3')">FOLDER #3</a><br />
<div class="folderCollapsed" id="folder_3">
folder three contents
</div>
 
 
 
</body>
</html>

Open in new window

Comment
Watch Question

Do more with

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

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

In the DOM, the first time around ....style.display 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

Author

Commented:
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