Solved

Jquery accordion toggle question

Posted on 2010-09-07
4
558 Views
Last Modified: 2012-05-10
I want to be able to toggle an active selection to close.  What code needs to be added to do this? Right now the script closes the previous onclick of the next selection..I want to keep that functionality.

</script>
<link rel="stylesheet" href="css/accordian.css" type="text/css"  />

<script type="text/javascript">
$(document).ready(function(){
      
//Set default open/close settings
$('.acc_container').hide(); //Hide/close all containers
$('.acc_trigger:first').addClass('active').next().hide(); //Add "active" class to first trigger, then show/open the immediate next container

//On Click
$('.acc_trigger').click(function(){
      if( $(this).next().is(':hidden') ) { //If immediate next container is closed...
            $('.acc_trigger').removeClass('active').next().slideUp(); //Remove all .acc_trigger classes and slide up the immediate next container
            $(this).toggleClass('active').next().slideDown(); //Add .acc_trigger class to clicked trigger and slide down the immediate next container
      }
      return false; //Prevent the browser jump to the link anchor
});

 

});
</script>
 

Thank you
0
Comment
Question by:vmoore99
  • 2
4 Comments
 
LVL 14

Expert Comment

by:Dhanasekaran Sengodan
ID: 33625580
0
 
LVL 15

Accepted Solution

by:
SRigney earned 250 total points
ID: 33626963
you need to set the option collapsible to true
http://jqueryui.com/demos/accordion/#option-collapsible

$( ".selector" ).accordion({ collapsible: true });
0
 

Author Comment

by:vmoore99
ID: 33640396
So I added per the below:
      else if( $(this).next().is(':visible') ) { //If immediate next container is closed...
            $('.acc_trigger').removeClass('active').next().slideUp(); //Remove all .acc_trigger classes and slide up the immediate next container

And it works....I tried to work with the examples and the other info from SRigney but I could not follow how that would work in my existing code.   I know I need to do some training with Javascript and Jquery.  I am really green.  If there is an issue with what I have done could someone please add that comment.




<script type="text/javascript">
$(document).ready(function(){
      
//Set default open/close settings
$('.acc_container').hide(); //Hide/close all containers
$('.acc_trigger:first').addClass('active').next().hide(); //Add "active" class to first trigger, then show/open the immediate next container
 

//On Click
$('.acc_trigger').click(function(){
      if( $(this).next().is(':hidden') ) { //If immediate next container is closed...
            $('.acc_trigger').removeClass('active').next().slideUp(); //Remove all .acc_trigger classes and slide up the immediate next container
            $(this).toggleClass('active').next().slideDown(); //Add .acc_trigger class to clicked trigger and slide down the immediate next container
      }
      
      else if( $(this).next().is(':visible') ) { //If immediate next container is closed...
            $('.acc_trigger').removeClass('active').next().slideUp(); //Remove all .acc_trigger classes and slide up the immediate next container
             
      }

      
      return false; //Prevent the browser jump to the link anchor
});

 

});
</script>

Thank you so very much for your patience.
0
 
LVL 15

Expert Comment

by:SRigney
ID: 33641079
I'm sorry I skimmed over your code or I would have noticed that you were writing your own Accordian.  I was linking you into the setting needed when using the JQuery UI plugin to implement the accordian.   The collapse setting is one that is frequently overlooked.

You may still want to look into using the jQuery UI plugin to reduce the amount of code you have to write yourself and the chance that you introduce a defect.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
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)
The viewer will learn the basics of jQuery including how to code hide show and toggles. 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…

867 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

12 Experts available now in Live!

Get 1:1 Help Now