Solved

Jquery Accordian - close before open

Posted on 2011-02-15
2
400 Views
Last Modified: 2013-11-18
I have the following code for an accordion menu, only problem is that it works fine the first time around, but then I have to double click the links before the open one closes and the clicked one opens.

All I want is :

When the anchor is clicked, check for open menus, if there are open ones, close them, then open the one thats clicked and add the class open.


$(function () {

	$('#pia-menu ul').hide();
	$('#pia-menu li a').click(function() 
		{
	    	if ($(this).hasClass("open")){
	    		
	    		$(this).next().slideUp('normal');	
                $(this).removeClass('open');	
	    	
	    	} else {
	    		
	    		$('.open').next().slideUp();
	    		$(this).next().slideDown('normal');	
                $(this).addClass('open');
	    	
	    	}
   
      	 });
});

Open in new window

0
Comment
Question by:HankMoody
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 40

Expert Comment

by:gurvinder372
ID: 34895714
check this demo here
http://jqueryui.com/demos/accordion/

when you click on an accordian option, it first closes the currently opened, and then open current one.

Let me know if i missed out on anything
0
 
LVL 16

Accepted Solution

by:
Steve Krile earned 250 total points
ID: 34899344
I would go for something like this:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

<style type="text/css">
`	body {width:100%;padding:0;margin:0;font-family:"Arial";}
	ul {padding:0;margin:0;}
	li {display:block;height:24px;border:solid 1px gray;width:200px;background:#ffc;text-align:center: padding:2px;}
	li.open {background:#ccc;}
	li a {text-decoration:none;display:inline-block;width:100%;outline: 0;text-align:center;}
</style>

<script src="http://code.jquery.com/jquery-1.4.2.min.js" language="javascript"></script>

</head>

	<ul id="pia-menu">
		<li><a href="#">Item 1</a></li>
		<li><a href="#">Item 2</a></li>
		<li><a href="#">Item 3</a></li>
	</ul>

<body>


	<script language="javascript">
		var oneOpen = false;
		$("#pia-menu li a").click(function(e){

		   e.preventDefault();
		   var newTab = $(this).parent()

			$("#pia-menu li.open").stop().animate({height: "24px"}).removeClass("open");
			newTab.stop().animate({height: "150px"}).addClass("open");




		})

	</script>
</body>
</html>

Open in new window

0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying 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

Suggested Solutions

Introduction JSON is an acronym for JavaScript Object Notation.  It is a text-string data transport mechanism, capable of representing simple or complex data structures in a consistent and easy-to-read manner.  Similar in concept to XML, but more e…
This article discusses how to implement server side field validation and display customized error messages to the client.
The purpose of this video is to demonstrate how to set up the WordPress backend so that each page automatically generates a Mailchimp signup form in the sidebar. This will be demonstrated using a Windows 8 PC. Tools Used are Photoshop, Awesome…
The viewer will learn how to dynamically set the form action using jQuery.

726 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