Help with some collapsable lists

Hi jQuery experts,

I need some advice, this is my first time using jQuery after reading book “jQuery Novice to Ninja” (very good book).

What I have is basically a list of items that when clicked they show/hide their content which is exactly what I want the problem is that I also want to control these lists with some buttons and what I want is that when any of these buttons is clicked to show the content of the target list and hide the rest of them, which is also working except when the target is closed, this is where I need help with how can I tell it to open it if its close and hide the rest of the lists.

In other words what I want is that if “button 1” is clicked to show only content from list1 and close list2 and list3 regardless of their current state (close/open), because right now if I click list1 and close it, when I click button 1 it hides all lists but it doesn’t show content of list1 since I’m using the “:not” selector.

Any idea how can I do this? Sorry if I’m not explaining this correctly.

Here is the code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<head>
<title>Testing </title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js" type="text/javascript"></script>
   <script type="text/javascript"> 

$(document).ready(function(){    
	
$('h3').click(function() {$(this).next(".list-items").animate({'height':'toggle'}, 'slow'); });  

$('.main-button').click(function(){$(".list-items").animate({'height':'toggle'}, 'slow'); });  
    
$('#button1').click(function() {$(".list-items:not(.list1)").hide(); });  
$('#button2').click(function() {$(".list-items:not(.list2)").hide(); }); 
$('#button3').click(function() {$(".list-items:not(.list3)").hide(); });      
   
});  
 
</script> 
 
</head>
<body>  
	<a class="main-button">Collapse/Open all</a>
  	      <ul>
			    <li id="button1"><a>Button 1</a></li>
			    <li id="button2"><a>Button 2</a></li> 
			    <li id="button3"><a>Button 3</a></li>      
			    </ul>

		  <h3>LIST 1</h3>
      <div class="list-items list1">
	   		<p>Some Text Here </p>
	   		<p>Some Text Here </p> 
	   		<p>Some Text Here </p> 
				</div>
 
			<h3>LIST 2</h3> 
			<div class="list-items list2">
	   		<p>Some Text Here </p> 
	   		<p>Some Text Here </p> 
	   		<p>Some Text Here </p> 
				</div>

		  <h3>LIST 3</h3>
      <div class="list-items list3">
	   		<p>Some Text Here </p> 
	   		<p>Some Text Here </p> 
	   		<p>Some Text Here </p> 
				</div>

	 </body>
</html> 

Thanks a lot

The standard technique to achieve that is to first close all of them, after which you then open up the appropriate one of them.

Thank you for your comments! I was just trying to mimic a single page like menu using the buttons on top, plus I wanted to have the complete list open since this is a restaurant menu and I wanted to have it opent as the default.

I guess it make sense to go the other way.

Thanks a lot for your help!