Solved

JavaScript Expand/Collapse one at a time

Posted on 2008-10-21
2
455 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
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…

759 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now