Solved

Jquery accordion toggle question

Posted on 2010-09-07
4
572 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
[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
4 Comments
 
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

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

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

Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
This article discusses how to implement server side field validation and display customized error messages to the client.
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…
Suggested Courses

624 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