Solved

Jquery Accordian - close before open

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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

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…
How to build a simple, quick and effective accordion menu using just 15 lines of jQuery and 2 css classes
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)

760 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

20 Experts available now in Live!

Get 1:1 Help Now