Bootstrap dropdown closing when clicked

JavascriptHtmlDrop Down-MenuTwitter Bootstrap

Javascript Problem Overview


I put a form inside a bootstrap dropdown, but when I click any of the fields in the form, the dropdown goes away. I have this piece of code but I don't know where to put it to prevent the dropdown disappearing.

$(function test() {
  // Setup drop down menu
  $('.dropdown-toggle').dropdown();

  // Fix input element click problem
  $('.dropdown input, .dropdown label').click(function(e) {
    e.stopPropagation();
  });
});

Could someone please tell me where I should put this? I tried in the bootstrap-dropdown, I tried within the HTML but it still doesn't work.

Javascript Solutions


Solution 1 - Javascript

You can omit the dropdown call all together, the bootstrap dropdown works without it. Make sure you're wrapping your script properly, you can include it on the header or body of your page, although personally i prefer placing it on the body of your page.

JS

<script type="text/javascript">
	$('.dropdown-menu input, .dropdown-menu label').click(function(e) {
		e.stopPropagation();
	});
</script>

Solution 2 - Javascript

If you want to stop closing only some of dropdown menu, not all of them, then just add an additional class to your ul block:

<ul class="dropdown-menu keep-open-on-click">

And use this code:

$(document).on('click', '.dropdown-menu', function(e) {
    if ($(this).hasClass('keep-open-on-click')) { e.stopPropagation(); }
});

Solution 3 - Javascript

Time passed since answer had been accepted, but if you want to keep the dropdown opened, if it acts like a kind of dialog, the best way i think is:

$('.dropdown').dropdown().on("hide.bs.dropdown", function(e) {
            if ($.contains(dropdown, e.target)) {
                e.preventDefault();
            //or return false;
            }
        });

Solution 4 - Javascript

You need to stop event from bubbling up the DOM tree:

$('.dropdown-menu').click(function(e) {
    e.stopPropagation();
});

event.stopPropagation prevents event from reaching the node where it's eventually handled by Bootstrap hiding menu.

Solution 5 - Javascript

This works for my (lateral sidebar opening a simple link with bootstrap toggle)

$('.dropdown').find("a").not('.dropdown-toggle').on("click",function(e){
	e.stopImmediatePropagation();
});

Solution 6 - Javascript

Thank you for the above answer, I was having the same issue w/ submenus under the dropdown menus. Using the above solution I was able to solve the problem for this as well.

$('.dropdown-menu .dropdown-submenu a[data-toggle="dropdown-submenu"]').click(function (e)
{					
	e.stopPropagation();
});

Solution 7 - Javascript

Put

<script type="text/javascript">
    $('.dropdown-menu').click(function(e) {
          e.stopPropagation();
    });
</script>

on the original answer, remove all the classes, and just put the ".dropdown-menu" class, it worked for me. i have an input field inside the dropdown menu.

Screenshot of menu

Solution 8 - Javascript

If you look to the bottom of sources of dropdown widget, you will see this:

$(document)
  .on('click.bs.dropdown.data-api', '.dropdown form', function(e) {
    e.stopPropagation()
  })

So you have the following choices:

>1. Simply wrap yor dropdown with form >2. Or you can add event listener for click event of .dropdown YOUR_SELECTOR

$(document)
  .on('click.my', '.dropdown some_selector', function(e) {
    e.stopPropagation()
  })

Solution 9 - Javascript

Simply use this example.This solution works for me.

<script type="text/javascript">
window.addEvent('domready',function() {    
   Element.prototype.hide = function() {
      $(function () { 
        // replace your tab id with myTab
        //<ul id="myTab" class="nav nav-tabs">
        $('#myTab li:eq(1) a').tab('show');
      }); 	   
   };
});
</script>

Hope it'll help. Thanks.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSorin CiobanView Question on Stackoverflow
Solution 1 - JavascriptAndres IlichView Answer on Stackoverflow
Solution 2 - Javascripto139View Answer on Stackoverflow
Solution 3 - JavascriptlavrikView Answer on Stackoverflow
Solution 4 - JavascriptVishnu T AsokView Answer on Stackoverflow
Solution 5 - JavascriptmanuelprojectsView Answer on Stackoverflow
Solution 6 - JavascriptusugarbageView Answer on Stackoverflow
Solution 7 - JavascriptRiver CR PhoenixView Answer on Stackoverflow
Solution 8 - JavascriptTimofey NovitskiyView Answer on Stackoverflow
Solution 9 - JavascriptShipluView Answer on Stackoverflow