1

I have one .navbar-toggler that I wish to target multiple toggleable navbars with.

I found this questions that is exactly the same but for Bootstrap 3. The solution there was simply to use the data-target attribute of the toggler with a class, and giving that class to all the navbar to be toggled.

This solution does not work for me. Even after putting a class selector as data-target, only the first found element is affected.

Is this a change from v3 to v4? Or is there something I'm not doing right?

This is my button:

<button class="navbar-toggler hidden-lg-up" type="button" data-toggle="collapse" data-target=".exCollapsingNavbar" aria-controls="exCollapsingNavbar" aria-expanded="false" aria-label="Toggle navigation">&#9776;</button>

And both menus have the exact same classes, namely:

'collapse navbar-toggleable-md exCollapsingNavbar'
Community
  • 1
  • 1
VesterDe
  • 160
  • 2
  • 13

2 Answers2

1

Bootstrap 4 is still in Alpha and this is one of the bugs around.

https://github.com/twbs/bootstrap/issues/19813

You can still use Javascript though.

Here's a quick alternative with JavaScript:

https://jsfiddle.net/e9xs33pa/2/

HTML:

<button class="navbar-toggler hidden-lg-up" type="button" aria-controls="exCollapsingNavbar" aria-expanded="false" aria-label="Toggle navigation" id="MyID">&#9776;</button>

<div class="collapse navbar-toggleable-md exCollapsingNavbar">Test</div>
<div class="collapse navbar-toggleable-md exCollapsingNavbar">Test 2</div>

JS:

jQuery('#MyID').on('click', function(){
    jQuery('.exCollapsingNavbar').collapse('toggle');
});

Basically, you give the button an ID of MyID. Have jQuery collapse toggle the exCollapsingNavbar class when the button is clicked.

Shiv
  • 831
  • 1
  • 12
  • 29
  • Thanks for the github link, I didn't think to check there first... And thanks for the js solution, although I would have preferred it if I were wrong, not Bootstrap :P – VesterDe Oct 27 '16 at 08:43
1

You can use multiple Toggler buttons on your web page with just using different ID's. Here is an example

Toggler one Code:

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

<div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav mr-auto">
        <li class="nav-item active">
          <a class="nav-link nav-link-color" href="#">
            Login<span class="sr-only">(current)</span></a
          >
        </li>
        <li class="nav-item active">
          <a class="nav-link nav-link-color" href="#">RSS Feed</a>
        </li></ul></div>

Toggler two Code: Here i just change the data-target value and aria-controls value with same as div ID as well.

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

<div class="collapse navbar-collapse" id="navbarSupportedContent2">
      <ul class="navbar-nav">
        <li class="nav-item active pr-3 " >
          <a class="nav-link nav-link-color text-uppercase hover2" href="#">
            style demo<span class="sr-only">(current)</span></a
          >
        </li>
        <li class="nav-item active pr-3">
          <a class="nav-link nav-link-color text-uppercase colorChange" href="#">full width</a>
        </li></ul></div>
azeem
  • 341
  • 3
  • 4