Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Close Bootstrap Dropdown after link click

I have a bootstrap dropdown menu below. It has a link in it hooked up to a knockout.js binding, that returns false because I don't want the # tag to be sent to the browser URL. However, doing this doesn't close the dropdown menu when I click the link. Any way around this?

HTML

<div class="btn-group">     <button class="btn dropdown-toggle" data-toggle="dropdown" data-bind="enable: !noResults()"><i class="icon-download-alt" ></i> Export <span class="icon-caret-down"></span></button>      <ul class="dropdown-menu">         @foreach(var exportUrl in Model.ExportUrls)         {             <li>                 <a href="#" data-bind="disable: noResults(), download: { url: '@exportUrl.Value', data: data }"><img src="/Content/less/images/img/@(exportUrl.Key.ToString().ToLower()).png" alt="@exportUrl.Key.GetDisplayName()"/> @exportUrl.Key.GetDisplayName()</a>             </li>         }     </ul> </div> 

knockut.js binding

ko.bindingHandlers.download = {     init: function (element, valueAccessor) {          var value = ko.utils.unwrapObservable(valueAccessor()),             id = 'download-iframe-container',             iframe;          $(element).unbind('click').bind('click', function () {              iframe = document.getElementById(id);              if (!iframe) {                 iframe = document.createElement("iframe");                 iframe.id = id;                 iframe.style.display = "none";             }              if (value.data) {                 iframe.src = value.url + (value.url.indexOf('?') > 0 ? '&' : '?') + $.param(ko.mapping.toJS(value.data));             } else {                 iframe.src = value.url;             }              document.body.appendChild(iframe);              return false;         });     } }; 
like image 542
Mike Flynn Avatar asked Sep 17 '13 16:09

Mike Flynn


People also ask

How do I close a bootstrap dropdown?

mouseleave(function () { $(". dropdown-toggle"). dropdown('toggle'); }); }); The problem with this is that it toggles all of the dropdowns on the page.

How do I stop dropdown menu Close menu items on clicking inside?

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() .

How do I close a drop-down?

If you have multiple cells with drop-down lists that you want to delete, you can use Ctrl+Left click to select them. Click Data >Data Validation. On the Settings tab, click Clear All.

How do I close a drop-down on click outside?

You can use the jQuery click() method in combination with the on() method to hide the dropdown menu when the user click outside of the trigger element.


2 Answers

Give your links a class (e.g. download):

<a href="#" class="download" data-bind="disable: noResults().... 

And your dropdown an id (e.g. dlDropDown):

<button class="btn dropdown-toggle" id="dlDropDown" data-toggle="dropdown" data-bind="enable: !noResults()"> 

And then add the following event handler:

$("a.download").click(function() {    $("#dlDropDown").dropdown("toggle"); }); 
like image 168
mccannf Avatar answered Oct 11 '22 14:10

mccannf


This will actually work with your existing bootstrap markup without having to add any new classes or ids. Hopefully this is helpful to someone else just looking to drop a solution in without changing anything existing.

$(".dropdown-menu a").click(function() {     $(this).closest(".dropdown-menu").prev().dropdown("toggle"); }); 
like image 38
brianespinosa Avatar answered Oct 11 '22 15:10

brianespinosa