Solved

Jquery accordion toggle question

Posted on 2010-09-07
4
556 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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

When you need to keep track of a simple list of numbers or strings, the Array object is your most direct tool.  As we saw in my earlier EE Article (http://www.experts-exchange.com/A_3488.html), typical array handling might look like this: (CODE) B…
The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
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…

759 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