Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

JavaScript Expand/Collapse one at a time

Posted on 2008-10-21
2
Medium Priority
?
498 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 2000 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

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.
This article discusses how to create an extensible mechanism for linked drop downs.
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…
Suggested Courses

916 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