Solved

JavaScript Expand/Collapse one at a time

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to hide html tags in text area 4 37
unable to set breakpoints in chrome source tab 1 31
Jquey and footrable 2 36
html form to write data to csv 24 97
When you need to keep track of a simple list of numbers or strings, the Array object is your most direct tool.  As we saw in my earlier EE Article (http://www.experts-exchange.com/A_3488.html), typical array handling might look like this: (CODE) B…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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…

867 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

13 Experts available now in Live!

Get 1:1 Help Now