2

Is there a way to make the jQuery accordion collapse when you click off of it? For example if you have one of the accordions open, if you click anywhere that isn't the accordion, then it closes.

jQuery

$(document).ready(function() {

    $( "#accordion" ).accordion({ 

        header: "h3",
        collapsible: true,
        active: false,
        heightStyle: "content",
        animate: {

            easing: "swing",
            duration: 300

        }

    });

});
Jbro
  • 269
  • 2
  • 8
  • possible duplicate of [How to detect a click outside an element?](http://stackoverflow.com/questions/152975/how-to-detect-a-click-outside-an-element) – Asad Saeeduddin May 02 '15 at 05:17

1 Answers1

3

How about this:

$(document).click(function (event) {
      if(!$(event.target).closest('#accordion').length) {//if you clicked outside of the accordion
             $("#accordion").accordion({active: false});//collapse all the panels
       }
 });
renakre
  • 8,001
  • 5
  • 46
  • 99