Discuss Toggle div - show only 1 div at a time in the JavaScript Development forum on Dev Shed. Toggle div - show only 1 div at a time JavaScript Development forum discussing JavaScript and DHTML, AJAX, and issues such as coding cross-browser JavaScript.
Posts: 247
Time spent in forums: 3 Days 9 h 8 m 22 sec
Reputation Power: 80
Toggle div - show only 1 div at a time
Hi folks,
I have this toggle script which works perfectly, however what I would like to incorporate into my original toggle code is for it to only allow one div to be shown at a time. So when one div is open and another link is clicked to open another div, the first div should then close and the second div should then appear in its place...
Code:
<!-- Begin
function toggle(itemID){
// Toggle visibility between none and inline
if ((document.getElementById(itemID).style.display == 'none'))
{
document.getElementById(itemID).style.display = 'inline';
} else {
document.getElementById(itemID).style.display = 'none';
}
}
// End -->
Can anyone please help to incorporate this??
Many thanks in advance for any help you are able to provide!
Posts: 2,557
Time spent in forums: 4 Weeks 5 h 34 m 26 sec
Reputation Power: 614
You need to organize a collection of all the divs you need to toggle on /off and circle trough them. But the reference is quite a custom job, thus we need to see your HTML code syntax of those divs in order to have a clear picture of your DOM structure.
Posts: 247
Time spent in forums: 3 Days 9 h 8 m 22 sec
Reputation Power: 80
Quote:
Originally Posted by KorRedDevil
You need to organize a collection of all the divs you need to toggle on /off and circle trough them. But the reference is quite a custom job, thus we need to see your HTML code syntax of those divs in order to have a clear picture of your DOM structure.
Now it will set all divs of class "toggle" to none, then set the desired div to inline.
Hey, yeah thanks for that! Works a charm! Although I can't seem to close any of the divs that are opened. Example - the "privacy" div is opened, but I dont want any opened at present, so when I click on the link to show "privacy" it doesn't "hide" the div...
Do you know how to work around this?
Many thanks for your help
EDIT: Never mind, I created a new function to close the opened div. Many many thanks for your help jhfry!! +rep
Last edited by terra-dea : May 13th, 2010 at 10:37 AM.