Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Jquery accordion toggle question

Posted on 2010-09-07
4
Medium Priority
?
577 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 1000 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone 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

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
This article discusses how to create an extensible mechanism for linked drop downs.
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…

705 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