62

When I shrink the window and click the button it doesn't respond. How do I fix this?

<button type="button" 
        class="navbar-toggle" 
        data-toggle="collapse" 
        data-target=".navbar-collapse">

    <span class="sr-only">Toggle navigation</span>
    <span class="icon-bar"></span>
    <span class="icon-bar"></span>
    <span class="icon-bar"></span>
</button>

<div class="collapse navbar-collapse">
    <ul class="nav navbar-nav">
        <li>
            <a href="">Page 1</a>
        </li>
        <li>
            <a href="">Page 2</a>
        </li>
        <li>
            <a href="">Page 3</a>
        </li>
    </ul>

Libraries are jQuery 2.1.3 and bootstrap 3.3.1.

Web Developer
  • 333
  • 4
  • 17
Nikasv
  • 1,317
  • 5
  • 17
  • 33

10 Answers10

62

In Bootstrap 5 data attributes are now namespaced in the js plugin with "bs", so for example data-toggle will not work anymore and you will need to use data-bs-toggle instead.

Crasher
  • 2,211
  • 2
  • 18
  • 17
51

Demo: http://jsfiddle.net/u1s62Lj8/1/

You need the jQuery and Boostrap Javascript files included in your HTML page for the toggle to work. (Make sure you include jQuery before Bootstrap.)

<html>
   <head>
       // stylesheets here
       <link rel="stylesheet" href=""/> 
   </head>
   <body>
      //your html code here

      // js scripts here
      // note jquery tag has to go before boostrap
      <script src="https://code.jquery.com/jquery-2.1.3.min.js"></script>
      <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/js/bootstrap.min.js"></script>
   </body>
</html>
nightblade9
  • 354
  • 2
  • 15
indubitablee
  • 8,136
  • 2
  • 25
  • 49
  • 3
    Posting for a friend. Don't forget to make sure you've not disabled javascript in dev tools the other day like, err, my friend, did. Close dev tools and see if it works! d'oh! – billythekid Apr 18 '16 at 13:26
  • 1
    I had a master page and the reference of jquery libs were included in child pages but not in the master page. Your answered helped me finding the problem, Thank you! – Jamshaid K. Oct 03 '17 at 12:57
  • this didnt work – AlThePal78 Oct 13 '22 at 18:17
36

Your code looks great, the only thing i see is that you did not include the collapsed class in your button selector. http://www.bootply.com/cpHugxg2f8 Note: Requires JavaScript plugin If JavaScript is disabled and the viewport is narrow enough that the navbar collapses, it will be impossible to expand the navbar and view the content within the .navbar-collapse.

The responsive navbar requires the collapse plugin to be included in your version of Bootstrap.

<div class="navbar-wrapper">
      <div class="container">

        <nav class="navbar navbar-inverse navbar-static-top">
          <div class="container">
            <div class="navbar-header">
              <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
                <span class="sr-only">Toggle navigation</span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
              </button>
              <a class="navbar-brand" href="#">Project name</a>
            </div>
            <div id="navbar" class="navbar-collapse collapse">
              <ul class="nav navbar-nav">
                    <li><a href="">Page 1</a>
                    </li>
                    <li><a href="">Page 2</a>
                    </li>
                    <li><a href="">Page 3</a>
                    </li>

              </ul>
            </div>
          </div>
        </nav>

      </div>
    </div>
Dee
  • 704
  • 5
  • 6
31

In Bootstrap 5, change data-toggle to data-bs-toggle , and data-target to data-bs-target.

Notice that bs has been added to the data attributes in this release.

Spidy
  • 556
  • 5
  • 11
9

Remember load jquery before bootstrap js

Alberto
  • 1,099
  • 9
  • 8
3

Wasted several hours only to realize that viewport meta was missing from my code. Adding here just in case some one else misses it out.

As soon as I added this, the toggle started working fine.

<meta name="viewport" content="width=device-width, initial-scale=1">
bhaskarc
  • 9,269
  • 10
  • 65
  • 86
3

because u have to have jquery set-up to enable the toggling functionality of the toggler button. So, all u have to do is to add bootstrap.bundle.js before bootstrap.css:

<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>

<script src="https://cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-ho+j7jyWK8fNQe+A12Hb8AhRq26LrZ/JpcUGGOn+Y7RsweNrtN/tE3MoK7ZeZDyx" crossorigin="anonymous"></script>
baraa yusri
  • 469
  • 4
  • 7
2

don't load twice jquery & bootstrap libraries.

after removing duplicates, works fine.

Brian Sanchez
  • 832
  • 1
  • 13
  • 11
0

I had the same problem.

I had added jQuery and bootstrap in the incorrect order in my header files.

I had to load jQuery BEFORE bootstrap and this solved the issue.

-2

If you will change the ID then Toggle will not working same problem was with me i just change

<div class="collapse navbar-collapse" id="defaultNavbar1">
    <ul class="nav navbar-nav">

id="defaultNavbar1" then toggle is working

  • He was kinda right... if you change the ID, you'll need to change also `data-target=` in the button. I had removed the ID thinking it was useless... – the_nuts Oct 17 '20 at 21:03