Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Twitter Bootstrap dropdown not working

Any ideas why this Twitter Bootstrap dropdown isn't working?

Javascript in head:

<script src="/assets/js/bootstrap.js" type="text/javascript"></script>

Menu HTML:

             <li class="dropdown">
                <a data-target="#" data-toggle="dropdown" class="dropdown-toggle" href="#">Dropdown menu here...
                  <b class="caret"></b>
                </a>
                <ul class="dropdown-menu">
                  <li><a href="/app/1">Item A</a></li>
                  <li><a href="/app/2">Item B</a></li>
                  <li><a href="/app/3">Item C</a></li>
                </ul>
              </li>
like image 876
Snowcrash Avatar asked Sep 21 '12 17:09

Snowcrash


People also ask

Why is my bootstrap dropdown menu not working?

Solution : The dropdown should be toggled via data attributes or using javascript. In the above program, we have forgotten to add a data attribute so the dropdown is not working. So add data-bs-toggle="dropdown" to toggle the dropdown.

How to use dropdown Bootstrap?

To open the dropdown menu, use a button or a link with a class of . dropdown-toggle and the data-toggle="dropdown" attribute. The . caret class creates a caret arrow icon (), which indicates that the button is a dropdown.

Does bootstrap require jQuery?

Bootstrap 5 is designed to be used without jQuery, but it's still possible to use our components with jQuery. If Bootstrap detects jQuery in the window object it'll add all of our components in jQuery's plugin system; this means you'll be able to do $('[data-bs-toggle="tooltip"]').


3 Answers

Put the dropdown class on a div that is wrapping the li rather than on the li itself. Like this...

        <div class="dropdown">
        <a class="dropdown-toggle" data-toggle="dropdown" href="#">Dropdown trigger</a>
        <ul class="dropdown-menu" role="menu" aria-labelledby="dLabel">
         ...
        </ul>
        </div>

That example is from the bootstrap site

Your code should look like this...

    <!DOCTYPE html >
    <head>
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script>
    <script type="text/javascript" src="../plugins/js/bootstrap-dropdown.js"></script>
    <link href="../theme/bootstrap.css" rel=stylesheet type="text/css">

    </head>
    <div class="dropdown">
        <ul class="dropdown">
        <a data-target="#" data-toggle="dropdown" class="dropdown-toggle" href="#">Dropdown menu here...
          <b class="caret"></b>
        </a>
            <ul class="dropdown-menu">
              <li><a href="/app/1">Item A</a></li>
              <li><a href="/app/2">Item B</a></li>
              <li><a href="/app/3">Item C</a></li>
            </ul>
        </ul>
    </div>

I tried making a nested list, but it didn't work with bootstrap. I think it gets the two dropdowns confused.

like image 122
noel Avatar answered Oct 16 '22 19:10

noel


Make sure you are using the latest version of JQuery

like image 40
A Star Avatar answered Oct 16 '22 19:10

A Star


This is easily solved. The element wrapping your code is a li, but it needs to be a div. This works for me:

<div class="dropdown">
  <a data-target="#" data-toggle="dropdown" class="dropdown-toggle" href="#">Dropdown menu here...
    <b class="caret"></b>
  </a>
  <ul class="dropdown-menu">
    <li><a href="/app/1">Item A</a></li>
    <li><a href="/app/2">Item B</a></li>
    <li><a href="/app/3">Item C</a></li>
  </ul>
</div>

enter image description here

If you want it to look like a button, just add "btn" to the class list like this:

<a data-target="#" data-toggle="dropdown" class="dropdown-toggle btn" href="#">

My header section looks like this:

<link rel='stylesheet' type='text/css' href='css/bootstrap.min.css'>
<link rel='stylesheet' type='text/css' href='css/bootstrap-responsive.min.css'>
<script src='js/jquery-2.0.0.min.js' type='text/javascript'>
<script src='js/bootstrap.min.js' type='text/javascript'>

You may want to leave out the responsive css if you're not doing anything for mobile devices.

like image 41
Graham Avatar answered Oct 16 '22 18:10

Graham