Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Jquery accordion toggle question

Posted on 2010-09-07
4
Medium Priority
?
582 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 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: 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 Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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

963 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