Solved

JavaScript Expand/Collapse one at a time

Posted on 2008-10-21
2
465 Views
Last Modified: 2012-08-13
Hi Experts

I found the following code provided by Roonaan on an old Experts Exchange question http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_21937453.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>

Open in new window

0
Comment
Question by:willnjen
2 Comments
 
LVL 82

Accepted Solution

by:
hielo earned 500 total points
ID: 22771103
here you go:
<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="setAll('closed');getParentElement(this,'tbody').className='opened';return false;">+</a>
      <a class="whenOpened" href="#" onclick="setAll('closed');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="setAll('closed');getParentElement(this,'tbody').className='opened';return false;">+</a>
      <a class="whenOpened" href="#" onclick="setAll('closed');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="setAll('closed');getParentElement(this,'tbody').className='opened';return false;">+</a>
      <a class="whenOpened" href="#" onclick="setAll('closed');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>

Open in new window

0
 

Author Closing Comment

by:willnjen
ID: 31508486
Thanks for your speedy answer!!
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question