?
Solved

Jquery Accordian - close before open

Posted on 2011-02-15
2
Medium Priority
?
409 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 1000 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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

In this article you'll learn how to use Ajax calls within your CodeIgniter application. To explain this, I'll illustrate how to implement a simple contact form to allow visitors to send you an email through your web site.
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…
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)
Suggested Courses

743 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