I mostly work with PHP. I am not really a JavaScript guy at all, but in this day and age we all end up working with it a bit.
I'm trying to do something that seems like it should be fairly simple. Unfortunately, I am not having any success getting it to work.
I have several links on a page. I have a corresponding number of div's.
.topdiv {
display:none;
}
<a href="#" onclick="topdivOpen('topdiv1');return FALSE;">1st Div</a><br />
<a href="#" onclick="topdivOpen('topdiv2');return FALSE;">2nd Div</a><br />
<a href="#" onclick="topdivOpen('topdiv3');return FALSE;">3rd Div</a><br />
<div id="topdiv1" class="topdiv">1st Div</div>
<div id="topdiv2" class="topdiv">2nd Div</div>
<div id="topdiv3" class="topdiv">3rd Div</div>
The div's display in css are all set to "none". When I click a link, I want the corresponding div to display (set css display to "block".)
My script for a single div works perfectly:
function topdivOpen(div)
{
setDisplay(div);
}
function setDisplay(div)
{
var obj=document.getElementById(div);
obj.style.display = (obj.style.display == 'block') ? 'none' : 'block';
}
My problem arises when I try to loop through the div's so that when I click on one, the others (if displayed) get their display set to "none".
This was my most recent attempt:
function topdivOpen(div)
{
for(var x=0;x<=10;x++)
{
var otherDiv = "topdiv"+x;
if (otherDiv == div)
{
setDisplay(div);
}
else
{
hideDiv(otherDiv);
}
}
}
function setDisplay(div)
{
var obj=document.getElementById(div);
obj.style.display = (obj.style.display == 'block') ? 'none' : 'block';
}
function hideDive(otherDiv)
{
var otherobj = document.getElementById(otherDiv);
if (otherobj)
{
otherobj.style.display == 'none';
}
}
Any thoughts would be welcome!
Thank you.