67

I am using a Bootstrap dropdown menu. The problem is that it never drops down upon the first click; I need to click 2 times for it to be toggled. I guess the click event is somehow getting stuck somewhere before propagating down...

Is there any way to fix that?

Jago
  • 2,751
  • 4
  • 27
  • 41

14 Answers14

89

In Bootstrap 4, bootstrap.min.js and bootstrap.bundle.min.js now conflict in dropdown.

By removing bootstrap.min.js, the dropdown double click issue will resolved.

Tom
  • 2,830
  • 1
  • 18
  • 22
Devil Bro
  • 921
  • 6
  • 4
85

If someone is using angular with ui-bootstrap module along with normal bootstrap HTML dropdown definition, there are also two clicks needed.

<li class="dropdown">
   <a href="#" class="dropdown-toggle" data-toggle="dropdown">Dropdown</a>
   [...]
</li>

=> Removing the data-toggle="dropdown" will fix the issue.

Opening the dropdown with one click will work after this.

Reference: https://github.com/angular-ui/bootstrap/issues/2294

minni
  • 1,144
  • 11
  • 9
  • WOW!!! I have exactly the same issue! I'm using ui-bootstrap and angular. Works well know.. thanks for sharing!! – user1322092 Oct 11 '14 at 19:11
  • Thanks. Worked like a charm :) – haotang Nov 15 '14 at 08:28
  • 2
    This was fixed in UI-Bootstrap 0.12. To anybody who had this problem after having upgraded to 0.12, simply add back `data-toggle="dropdown"` as you've removed previously. – kba Nov 25 '14 at 22:20
  • What about [this](https://stackoverflow.com/questions/24218663/avoid-having-to-double-click-to-toggle-bootstrap-dropdown#answer-50253521), [this](https://stackoverflow.com/questions/24218663/avoid-having-to-double-click-to-toggle-bootstrap-dropdown#answer-52554398), [this](https://stackoverflow.com/questions/24218663/avoid-having-to-double-click-to-toggle-bootstrap-dropdown#answer-52569748) or [this](https://stackoverflow.com/questions/24218663/avoid-having-to-double-click-to-toggle-bootstrap-dropdown#answer-52848502)? There are issues by using only the bootstrap.bundle.min.js? – Backo Nov 27 '18 at 13:34
  • 2020 and angular 9.1.7: this anwer still saved me. In angular 9.0.7 the issue didn't exist. – briosheje May 25 '20 at 12:24
  • 2021 only the removing of the `bootstrap.bundle.min.js` solved the issue, drawback still unknown – Artem Kozlenkov Feb 24 '21 at 10:38
36

This may happen if somehow Bootstrap is included twice in your project.

If bootstrap.min.js and bootstrap.bundle.min.js(bootstrap and popper js) both included in project, it means redundant bootstrap js.

The js combination should be like this: Either : bootstrap.bundle.min.js only Or : bootstrap.min.js AND popper.min.js For detailed information, visit https://getbootstrap.com/docs/4.1/getting-started/contents/

Rajaneesh Singh
  • 361
  • 3
  • 4
  • 1
    It seems to solve the problem, but what are the implications e.g. of using only the bootstrap.bundle.min.js? – Backo Nov 27 '18 at 00:40
  • bootstrap.bundle.min.js comes with some extra features along with bootstrap js like popperjs. If your application is using popperjs and bootstrap, you can include the bootstrap.bundle.min.js. If not, you don't want to include bundle js with the larger size. – PraBhu May 10 '20 at 11:27
  • Super answer. Was using a datatables bundle AND separate bs4 include so removing it from the bundle fixed it. Thanks. – Antony Jan 15 '21 at 12:08
14

In BootStrap 4, One should not include both "bootstrap.bundle.min.js" and "bootstrap.min.js". This will cause the DropDown issue. Only Keep "bootstrap.bundle.min.js" as it will have all required code.

PraBhu
  • 196
  • 3
  • 13
  • thx fixed the problem, I thought that somewhere a double event – jmp Dec 10 '19 at 11:43
  • And that's the real winner. Solves this issue complete. Also keep in mind to load jquery before bootstrap. In my case jQuery 3.4.1 and Bootstrap 4.3.1. – Harm May 07 '20 at 12:53
10

This happens because the Twitter Bootstrap syntax for their drop downs include a href tag. You will need to preventDefault and stopPropagation to prevent this from happening. Something like this will do the trick:

$('.dropdown-toggle').click(function(e) {
    e.preventDefault();
    e.stopPropagation();

    return false;
});
rdougan
  • 7,217
  • 2
  • 34
  • 63
  • Perfect! However I am somehow surprised they don't already include this in their bootstrap js file. – Jago Jun 20 '14 at 03:14
  • 2
    I'm guessing that when using just Bootstrap, using href is the expected behaviour. However when using Angular, you don't want to use that. It sucks, though. – rdougan Jun 20 '14 at 10:33
6

Like @rajaneesh-singh told us above, this bug occurs when Bootstrap is included twice.

In my case, I had a duplicate with the offical bootstrap and with the twitter version:

<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.1/js/bootstrap.js"></script>

I just removed the twitter version and everything is working well now.

b126
  • 544
  • 4
  • 11
4

I had the same issue as jaybro in https://stackoverflow.com/a/27278529/1673289, however their solution didn't help.

The way to fix for both cases was to add:

data-disabled="true"

onto the element which has the data-toggle="dropdown" attribute.

Community
  • 1
  • 1
JoshSub
  • 499
  • 1
  • 4
  • 11
4

I solved the issue by removing link to bootstrap.bundle.js and adding a link to popper.js

Before

<script src="bootstrap/jquery/jquery.js"></script>
<script src="bootstrap/js/bootstrap.bundle.js"></script>
<script src="bootstrap/js/bootstrap.js"></script>

After

<script src="bootstrap/jquery/jquery.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>
<script src="bootstrap/js/bootstrap.js"></script>

Note that I downloaded the bootstrap files and stored them in a directory called 'bootstrap'. Inside it I had three different directories 'css', 'js', 'jquery'.

Nishant Ingle
  • 793
  • 1
  • 8
  • 12
4

if use bootstrap4, remove bootstrap.bundle.min.js. I had the same problem and it was fixed

hadi.sh
  • 129
  • 1
  • 4
3

In my case, the other answers don't work.

In application.js, I had:

//= require popper.min
//= require bootstrap-sprockets

Removing bootstrap-sprockets fixed the problem.

sscirrus
  • 55,407
  • 41
  • 135
  • 228
3

I came across the same issue recently, i solved it by writing custom script:

   <div class="filter-dropdown text-right">
      <button type="button" class="btn btn-secondary dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Edit</button>
      <div class="dropdown-menu">
         <a class="dropdown-item" href="#">Link 1</a>
         <a class="dropdown-item" href="#">Link 2</a>
         <a class="dropdown-item" href="#">Link 3</a>
      </div>
    </div>



    $(".filter-dropdown").on("click", ".dropdown-toggle", function(e) { 
        e.preventDefault();
        $(this).parent().addClass("show");
        $(this).attr("aria-expanded", "true");
        $(this).next().addClass("show"); 
      });
Lovin Nagi
  • 94
  • 4
2

This may also happen if you have more than one bootstrap.js files! Check if you still have an older version and remove that script.

1

By looking within the source code of angular.ui.bootstrap and the native bootstrap.js they have both handlers for dropdowns.

Proposed Solution: adjust angular.ui.bootstrap dropdownToggle directive restrict only to "A" attributes. so by resolving this issue you need to modify dropdownToggle restrict "CA" into "A" this will hide Class looker from angular.ui.bootstrap which has been already handled by bootstrap.js

for the minified version of angular.ui.bootstrap look for this line directive("dropdownToggle",function(){return{restrict:"CA",require:"?^dropdown",link:function(a,b,c,d){

replace "CA" with "A".

Cheers

Jomaf
  • 91
  • 1
  • 4
0

I found that on some pages I would have to double click and other pages were single click.

Removing data-toggle="dropdown" made the double clicks into single and broke the singles.

I compared the html in the dev tool (because the same html is being served) and found on my double click pages the div with data-toggle looked like this:

<div id="notifications" data-toggle="dropdown" class="dropdown-toggle"
     aria-haspopup="true" aria-expanded="false">

But the single click html looks like this:

<div id="notifications" data-toggle="dropdown" class="dropdown-toggle">

So, in the document ready, I used attr detection and the prevent default answer and it made my double click opens into single click opens:

if (!($('#notifications').attr('aria-haspopup') == undefined &&
    $('#notifications').attr('aria-expanded') == undefined))
{
    $('.dropdown-toggle').click(function (e) { return false; });
}
jaybro
  • 1,363
  • 1
  • 12
  • 23