Solved

Jquery Accordian - close before open

Posted on 2011-02-15
2
390 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
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer …
Introduction Chart.js, used properly, can visually add a difference to your charting applications. It engages your visitors and allows them to interact with data they otherwise wouldn't be able to without expensive and complicated systems. For this…
The viewer will learn how to dynamically set the form action using jQuery.
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)

864 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

25 Experts available now in Live!

Get 1:1 Help Now