Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bootstrap Navbar toggle button not working

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.

like image 867
Nikasv Avatar asked Jan 13 '15 19:01

Nikasv


People also ask

Why is my toggle button not working in bootstrap?

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.

How do I toggle a button in bootstrap?

Add data-toggle="buttons" to a . btn-group containing those modified buttons to enable their toggling behavior via JavaScript and add . btn-group-toggle to style the <input> s within your buttons. Note that you can create single input-powered buttons or groups of them.

How does navbar collapse work?

The data-toggler= “collapse” is used with navbar-toggler for the working button. This button helps to hide and show the items of the navbar. collapse navbar-collapse class is placed in the main div tag. This class is used for control and cover all the content Navbar with collapsing.


2 Answers

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> 
like image 106
indubitablee Avatar answered Sep 21 '22 12:09

indubitablee


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> 
like image 27
Dee Avatar answered Sep 20 '22 12:09

Dee