Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bootstrap4 dropdown only works in the second click

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>
like image 500
gbrennon Avatar asked May 23 '18 17:05

gbrennon


2 Answers

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

like image 199
Iwan B. Avatar answered Oct 13 '22 11:10

Iwan B.


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>
like image 24
mooga Avatar answered Oct 13 '22 13:10

mooga