Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery Accordion Expand All Collapse All

I was looking for a way to include an "expand all" and "collapse all". I've updated the demo with the new code using a simple jquery accordion.

The original code should be credited to Ryan Stemkoski at http://www.stemkoski.com/stupid-simple-jquery-accordion-menu/

Demo: http://jsbin.com/ucalu3/5/

$(document).ready(function() { 
  $('.question').click(function() {

  if($(this).next().is(':hidden') != true) {
                $(this).removeClass('active'); 
    $(this).next().slideUp("normal");
  } else {
    $('.question').removeClass('active');  
     $('.answer').slideUp('normal');
    if($(this).next().is(':hidden') == true) {
    $(this).addClass('active');
    $(this).next().slideDown('normal');
     }   
  }
   });

  $('.answer').hide();

  $('.expand').click(function(event)
    {$('.question').next().slideDown('normal');
        {$('.question').addClass('active');}
    }
  );

  $('.collapse').click(function(event)
    {$('.question').next().slideUp('normal');
        {$('.question').removeClass('active');}
    }
  );
});
like image 994
Evan Avatar asked Oct 06 '10 11:10

Evan


People also ask

How do you expand accordion?

By default, accordion items expand or collapse by clicking the accordion item header or clicking expand/collapse icon in accordion header. You can also expand or collapse the accordion items through external button click.

What is the jQuery code for collapsible content?

Basic collapsible To create a collapsible block of content, create a container and add the data-role="collapsible" attribute. Directly inside this container, add any header (H1-H6) or legend element.

How do I close accordion on click?

By default, there is no option to close the accordion on click but there is a solution. Add the code to Theme options General options Custom CSS/JS Custom JS field. Note, in case you have links inside the accordion they will not be clickable, they will close the accordion.

What is accordion jQuery?

jQuery - Widget accordion Accordion is same like as Tabs,When user click headers to expand content that is broken into logical sections.


2 Answers

This can be resolved much easier.

Simply use the jQuery hide/show command on the accordion element ('.ui-widget-content') you want to expand/collapse.

example:

$(document).ready(function() {
        $('.expand').click(function() {
            $('.ui-widget-content').show();
        });
        $('.collapse').click(function() {
            $('.ui-widget-content').hide();
        });
});

See fiddle: http://jsfiddle.net/ekelly/hG9b5/11/

like image 64
user2500414 Avatar answered Sep 20 '22 17:09

user2500414


I would add a class or ID to the expand and collapse links then something like this will work

$(document).ready(function() {
  $("#expand").click(function(){
    ('.answer').slideDown('normal');
  });

  $("#collapse").click(function(){
    ('.answer').slideUp('normal');
  });
}
like image 28
Sean Avatar answered Sep 20 '22 17:09

Sean