3

I am trying to put checkboxes in a bootstrap dropdown. I do not want the dropdown to close on click but I still want it close if they click else where on the website. I still want to fire off other javascript actions on click with in the dropdown. I have an example of the exact inverse of what I want. This example closes the drop down on click but keeps it open when clicked from the outside the dropdown area.

 $(function () {
     $('.dropdown.keep-open').on({
         "shown.bs.dropdown": function() {
             $(this).data('closable', false);
         },
         "click": function() {
             $(this).data('closable', true);
         },
         "hide.bs.dropdown": function() {
             return $(this).data('closable');
         }
     });
 });   

http://jsfiddle.net/KyleMit/ZS4L7/

Mike Causer
  • 8,196
  • 2
  • 43
  • 63
user1978109
  • 727
  • 1
  • 8
  • 19

2 Answers2

6

I finally got it. the code is below:

$(function() {
    $('.dropdown.keep-open').on({
        "shown.bs.dropdown": function() {
            $(this).data('closable', false);
        },
        "click": function(event) {
            $(this).data('closable', false);
        },
        "hide.bs.dropdown": function(event) {
            temp = $(this).data('closable');
            $(this).data('closable', true);
            return temp;
        }
    });
});

Edit: Added missing semicolon on line 11.

yaserso
  • 2,638
  • 5
  • 41
  • 73
user1978109
  • 727
  • 1
  • 8
  • 19
-1

I had to achieve the same thing and wrote an even simpler one:

$('.dropdown.keep-open').one({
  'mouseenter': () => { $(this).data('closable', false); },
  'mouseleave': () => { $(this).data('closable', true); },
  'hide.bs.dropdown': () => !!$(this).data('closable')
});

I hope it helps someone. Btw, these are very dirty hacks, Bootstrap could really have an option for this.

tom
  • 2,137
  • 2
  • 27
  • 51