Jquery Accordion Expand All Collapse All

Evan picture Evan · Oct 6, 2010 · Viewed 19.3k times · Source

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');}
    }
  );
});

Answer

user2500414 picture user2500414 · Jul 3, 2013

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/