16

I am working on a django project, that is using bootstrap4 and I'm with a little problem with dropdown toggler.

The toggler only toggles the dropdown-menu after the second link!

What I've done wrong?

This is my dropdown HTML code:

            <li class="nav-item dropdown show">
                <a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                    Dropdown Toggler
                </a>
                <div class="dropdown-menu" role="menu" aria-labelledby="navbarDropdown">
                 <a class="dropdown-item">Link 1</a>
                 <a class="dropdown-item">Link 2</a>
                </div>
            </li>
peterh
  • 11,875
  • 18
  • 85
  • 108
gbrennon
  • 899
  • 2
  • 11
  • 30

7 Answers7

41

I had the same problem then resolved. Often I due to a double import of bootstrap js libraries. Hope will help you.

Iwan B.
  • 3,982
  • 2
  • 27
  • 18
  • Yeah, this was also my issue. I used Mix extract vendor and I added the whole boostrap.js there while only cherry picking some js components in my entry point and laravel mix was getting confused. – Stem Florin Apr 23 '19 at 08:05
  • proceeding with this answer by, I am using a Template which loads its own JS It also loaded bootstrap.js file, so had to remove previously added – Jitendra Sawant Apr 13 '20 at 08:37
  • This was my problem. I use Laravel, and the bootstrap js file is already exists in app.js. – Rouhollah Mazarei Feb 26 '21 at 07:10
15

you need to use the toggle like that and assign the data-target to the drop down menu with it's id

using

data-target="#navbarDropdown"

 <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarDropdown" aria-controls="navbarDropdown" aria-expanded="false" aria-label="Toggle navigation">
        <span class="navbar-toggler-icon"></span>
      </button>

and then assign that id to the menu

        <li class="nav-item dropdown show">
            <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarDropdown" aria-controls="navbarDropdown" aria-expanded="false" aria-label="Toggle navigation">
        <span class="navbar-toggler-icon"></span>
      </button>
            <div id="navbarDropdown" class="dropdown-menu"  role="menu" aria-labelledby="navbarDropdown">
             <a class="dropdown-item">Link 1</a>
             <a class="dropdown-item">Link 2</a>
            </div>
        </li>
mooga
  • 3,136
  • 4
  • 23
  • 38
4

I came across the same issue recently, i solved this issue by adding $('.dropdown-toggle').dropdown() after loading the drop-down. Hope this may help without changing from the data-toggle="dropdown" to data-toggle="collapse".

Arjun
  • 113
  • 2
  • 7
3

Try to put bootstrap js file before bootstrap css

Ex:

  1. Link Bootstrap.min.js file first.

  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script>
 
  1. Then link css file

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">

I got the same issue and after changing the positions of the js and css, dropdown works well. Hope it helped you.

Nimezzz
  • 1,814
  • 14
  • 15
  • normally you shouldn't load your scripts in ``, but I would refer you to [this comprehensive answer](https://stackoverflow.com/a/24070373/4770813) for a modern approach – Scaramouche Nov 05 '18 at 04:22
2

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

I had the same issue, it occurred because i imported bootstrap library twice. After removing one of them it worked well. Hope it help you.

0

Agree with Iwan B. In Rails in javascripts I left only bootstrap.bundle.js. In Gemfile add gem 'popper_js', '~> 1.14.5', respectively in application.js we should add //= require popper