willnjen
asked on
JavaScript Expand/Collapse one at a time
Hi Experts
I found the following code provided by Roonaan on an old Experts Exchange question https://www.experts-exchange.com/questions/21937453/Expand-Collapse-JavaScript.html
It works perfectly expanding and collapsing content using javascript.
My question is, how can I alter this code so that only one thing can be open at one time? Something like, if the user clicks expand, close all other things open. Is that clear? The current code can have any number of things open, i want to restrict it to only one at a time.
There might be a completely different way of doing it. I'm not much of a coder so full examples appreciated.
I found the following code provided by Roonaan on an old Experts Exchange question https://www.experts-exchange.com/questions/21937453/Expand-Collapse-JavaScript.html
It works perfectly expanding and collapsing content using javascript.
My question is, how can I alter this code so that only one thing can be open at one time? Something like, if the user clicks expand, close all other things open. Is that clear? The current code can have any number of things open, i want to restrict it to only one at a time.
There might be a completely different way of doing it. I'm not much of a coder so full examples appreciated.
<html>
<head>
<title>SomeTitle</title>
<style type="text/css">
.closed .whenOpened {display:none;}
.opened .whenClosed {display:none;}
</style>
<script>
function setAll(state) {
var div = document.getElementById('myTable');
for(i = 0; i < div.childNodes.length; i++) {
if(div.childNodes[i].nodeType == 1) div.childNodes[i].className = state;
}
}
function getParentElement(elem, tag) {
tag = tag.toLowerCase();
while(elem && elem.nodeName.toLowerCase() != tag) {
if(!elem.parentNode) {
return null;
}
elem = elem.parentNode;
}
return elem;
}
</script>
</head>
<body>
<table id="myTable">
<tbody class="closed">
<tr><td>
<a class="whenClosed" href="#" onclick="getParentElement(this,'tbody').className='opened';return false;">+</a>
<a class="whenOpened" href="#" onclick="getParentElement(this,'tbody').className='closed';return false;">-</a>
<div id="1a" class="whenOpened">One.</div>
<div id="1b" class="whenOpened">Two.</div>
</td></tr>
</tbody>
<tbody class="closed">
<tr><td>
<a class="whenClosed" href="#" onclick="getParentElement(this,'tbody').className='opened';return false;">+</a>
<a class="whenOpened" href="#" onclick="getParentElement(this,'tbody').className='closed';return false;">-</a>
<div id="1a" class="whenOpened">One.</div>
<div id="1b" class="whenOpened">Two.</div>
</td></tr>
</tbody>
<tbody class="closed">
<tr><td>
<a class="whenClosed" href="#" onclick="getParentElement(this,'tbody').className='opened';return false;">+</a>
<a class="whenOpened" href="#" onclick="getParentElement(this,'tbody').className='closed';return false;">-</a>
<div id="1a" class="whenOpened">One.</div>
<div id="1b" class="whenOpened">Two.</div>
</td></tr>
</tbody>
</table>
<a href="" onclick="setAll('opened');return false;">Expand All</a> | <a href="" onclick="setAll('closed');return false">Contact All</a>
</body>
</html>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER