I have a Twitter Bootstrap dropdown menu. As all Twitter Bootstrap users know, the dropdown menu closes on click (even clicking inside it).
To avoid this, I can easily attach a click event handler on the dropdown menu and simply add the famous event.stopPropagation()
.
<ul class="nav navbar-nav">
<li class="dropdown mega-dropdown">
<a href="javascript:;" class="dropdown-toggle" data-toggle="dropdown">
<i class="fa fa-list-alt"></i> Menu item 1
<span class="fa fa-chevron-down pull-right"></span>
</a>
<ul class="dropdown-menu mega-dropdown-menu">
<li>
<div id="carousel" class="carousel slide" data-ride="carousel">
<ol class="carousel-indicators">
<li data-slide-to="0" data-target="#carousel"></li>
<li class="active" data-slide-to="1" data-target="#carousel"></li>
</ol>
<div class="carousel-inner">
<div class="item">
<img alt="" class="img-rounded" src="img1.jpg">
</div>
<div class="item active">
<img alt="" class="img-rounded" src="img2.jpg">
</div>
</div>
<a data-slide="prev" role="button" href="#carousel"
class="left carousel-control">
<span class="glyphicon glyphicon-chevron-left"></span>
</a>
<a data-slide="next" role="button" href="#carousel"
class="right carousel-control">
<span class="glyphicon glyphicon-chevron-right"></span>
</a>
</div>
</li>
</ul>
</li>
</ul>
This looks easy and a very common behavior, however, and since carousel-controls
(as well as carousel indicators
) event handlers are delegated to the document
object, the click
event on these elements (prev/next controls, ...) will be “ignored”.
$('ul.dropdown-menu.mega-dropdown-menu').on('click', function(event){
// The event won't be propagated up to the document NODE and
// therefore delegated events won't be fired
event.stopPropagation();
});
Relying on Twitter Bootstrap dropdown hide
/hidden
events is not a solution for the following reasons:
- The provided event for both event handlers doesn’t give you reference to the clicked element
- The dropdown menu content is dynamically generated so adding a
flag
class is not possible
This fiddle is the normal behavior and this fiddle is with event.stopPropagation()
added.
Update
Thanks to Roman for his answer. I also found an answer that you can find below.
I know this question was specifically for jQuery, but for anyone using AngularJS that has this problem you can create a directive that handles this:
Then just add the attribute
dropdown-prevent-close
to your element that is triggering the menu to close, and it should prevent it. For me, it was aselect
element that automatically closed the menu:});
Removing the data attribute
data-toggle="dropdown"
and implementing the open/close of the dropdown can be a solution.First by handling the click on the link to open/close the dropdown like this :
and then listening the clicks outside of the dropdown to close it like this :
Here is the demo : http://jsfiddle.net/RomaLefrancois/hh81rhcm/2/
I also found a solution.
Assuming that the
Twitter Bootstrap Components
related events handlers are delegated to thedocument
object, I loop the attached handlers and check if the current clicked element (or one of its parents) is concerned by a delegated event.Hope it helps any one looking for a similar solution.
Thank you all for your help.
I've got a similar problem recently and tried different ways to solve it with removing the data attribute
data-toggle="dropdown"
and listeningclick
withevent.stopPropagation()
calling.The second way looks more preferable. Also Bootstrap developers use this way. In the source file I found initialization of the dropdown elements:
So, this line:
suggests you can put a
form
element inside the container with class.dropdown
to avoid closing the dropdown menu.