1

I'm using this script to control open/collapse menu items:

$('.dropdown').on({
    "click": function(event) {
        if ($(event.target).closest('.dropdown-toggle').length && $(this).parents('.dropdown').length === ($(event.target).parents('.dropdown').length - 1)) {
            $(this).data('closable', true);
        } else {
            $(this).data('closable', false);
        }
    },
    "hide.bs.dropdown": function(event) {
        hide = $(this).data('closable');
        $(this).data('closable', true);
        return hide;
    }
});

But I need to create a conditional that if user click outside the menu, the open item not close.

This question involves Bootstrap specificities. Just using:

$(document).on('click', function(event)

Not answer my question completely.

marcelo2605
  • 2,734
  • 4
  • 29
  • 55
  • 1
    Possible duplicate of [How to detect a click outside an element?](http://stackoverflow.com/questions/152975/how-to-detect-a-click-outside-an-element) – daxro Apr 12 '16 at 13:26

1 Answers1

1

Try this to detect if you are out of the menu.

$(document).on('click', function(event) {
  if (!$(event.target).closest('.dropdown-toggle').length) {
      return false;
  }
});
E.Agolli
  • 552
  • 2
  • 11
  • I add $('.dropdown').on("hide.bs.dropdown", function(event) {return false; }); inside the function, but the menu still close when click in other part of the page. – marcelo2605 Apr 12 '16 at 13:43
  • Sorry i missed that you didnt want to close if clicked outside. In this case you can try with event.stopPropagation(); but im not sure if other things arent touched – E.Agolli Apr 12 '16 at 15:12
  • This script work in part. The problem it stop all other Bootstrap javascript module in page, lake panels and carousel. Any idea? – marcelo2605 Apr 13 '16 at 14:40
  • Nope. Now, the menu was close when click outside. – marcelo2605 Apr 13 '16 at 14:45