skip to Main Content

I am actually facing some problems to get an bootstrap accordion working inside a dropdown menu.
I googled a lot and also find some StackOverflow threads here, but they are several years old and depending on bootstrap 3 or bootstrap 2.
Like this one here: Twitter Bootstrap: How to create a dropdown button with an accordion inside it?

Can anybody help me here or have a solution for a working accordion inside a bootstrap dropdown menu.

<nav class="navbar navbar-expand-md">
  <ul class="navbar-nav">
    <li class="nav-item mega-dropdown-menu">
      <a class="nav-link" href="#" id="dropdown01" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Test</a>
      <div class="dropdown-menu" aria-labelledby="dropdown01">
        <nav class="navbar">
          <ul class="navbar-nav">
            <li class="nav-item">
              <a class="nav-link" data-toggle="collapse" href="#collapseExample" role="button" aria-expanded="false" aria- controls="collapseExample">Accordion</a>
              <div id="collapseExample" class="collapse">
                <ul class="navbar-nav">
                  <li class="nav-link">
                    <a class="nav-link" href="#">Accordion Link 1</a>
                  </li>
                  <li class="nav-link">
                    <a class="nav-link" href="#">Accordion Link 2</a>
                  </li>
                </ul>
              </div>
            </li>
          </ul>
        </nav>
      </div>
    </li>
  </ul>
</nav>

2

Answers


  1. try this

    // Prevent dropdown to be closed when we click on an accordion link
    $('.dropdown-accordion').on('click', 'a[data-toggle="collapse"]', function (event) {
      $($(this).attr('href')).collapse('toggle');
      event.stopPropagation();
    });
    <script src="http://code.jquery.com/jquery-3.3.1.slim.min.js"></script>
    <link href="http://stackpath.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css" rel="stylesheet"/>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.0/umd/popper.min.js"></script>
    <script src="http://stackpath.bootstrapcdn.com/bootstrap/4.1.0/js/bootstrap.min.js"></script>
    <link href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"/>
    <div class="container">                                        
      <div class="dropdown dropdown-accordion">
        <button type="button" class="btn btn-primary dropdown-toggle" data-toggle="dropdown">
          Dropdown
        </button>
     
        <div id="accordion" class="dropdown-menu">
        <div class="card dropdown-item">
          <div class="card-header">
            <a class="card-link" data-toggle="collapse" href="#collapseOne">
               Item #1
            </a>
          </div>
          <div id="collapseOne" class="collapse show" data-parent="#accordion">
            <div class="card-body">
              Lorem ipsum dolor sit amet
            </div>
          </div>
        </div>
        <div class="card dropdown-item">
          <div class="card-header">
            <a class="collapsed card-link" data-toggle="collapse" href="#collapseTwo">
            Item #2
          </a>
          </div>
          <div id="collapseTwo" class="collapse" data-parent="#accordion">
            <div class="card-body">
              Lorem ipsum dolor sit amet
            </div>
          </div>
        </div>
        <div class="card dropdown-item">
          <div class="card-header">
            <a class="collapsed card-link" data-toggle="collapse" href="#collapseThree">
               Item #3
            </a>
          </div>
          <div id="collapseThree" class="collapse" data-parent="#accordion">
            <div class="card-body">
              Lorem ipsum dolor sit amet
            </div>
          </div>
        </div>
      </div>
      </div>
    </div>
    Login or Signup to reply.
  2. The trick to keeping the Navbar dropdown open on click is to use a <form> tag. Therefore if you place the accordion inside a <form> it will work as desired.

    <div class="collapse navbar-collapse" id="navbarContent">
      <ul class="navbar-nav">
        <li class="nav-item dropdown">
          <a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
            Dropdown
          </a>
          <div class="dropdown-menu">
            <form class="accordion" id="accordionExample">
              <div>
                <a class="nav-link" data-toggle="collapse" data-target="#collapseOne" aria-expanded="true" aria-controls="collapseOne">
                  Item #1
                </a>
                <div id="collapseOne" class="collapse px-3 show" data-parent="#accordionExample">
                  Collapse 1 content
                </div>
              </div>
              <div>
                <a class="nav-link" data-toggle="collapse" data-target="#collapseTwo" aria-expanded="false" aria-controls="collapseTwo">
                  Item #2
                </a>
                <div id="collapseTwo" class="collapse px-3" data-parent="#accordionExample">
                  Collapse 2 content...
                </div>
              </div>
              <div>
                <a class="nav-link" data-toggle="collapse" data-target="#collapseThree" aria-expanded="false" aria-controls="collapseThree">
                  Item #3
                </a>
                <div id="collapseThree" class="collapse px-3" data-parent="#accordionExample">
                  Collapse 3 content...
                </div>
              </div>
            </form>
          </div>
        </li>
      </ul>
    </div>
    

    Demo: https://www.codeply.com/go/F8iQy0MeLa

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search