Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

href not working on dropdown-toggle - bootstrap

I fail to understand why href is not working on a tag for data-toggle="dropdown" . When I hit the Lists tab, i should be routed to the google website.

FIDDLE HERE

Simple HTML:

<div class="btn-group">
   <a href="http://google.com" class="btn btn-default dropdown-toggle" data-toggle="dropdown">Lists</a>

    <ul class="dropdown-menu" role="menu">
        <li>
            <a href="#">Sub List 1</a>
        </li>
        <li>
            <a href="#">Sub List 2</a>
        </li>
    </ul>
</div>
like image 307
user2281858 Avatar asked Dec 06 '15 20:12

user2281858


People also ask

Why can't I toggle the dropdown buttons in Bootstrap dropdown?

Reason 1: Forgot to include the popper.js/ popper.min.js file in the project code. In the following program, the dropdown buttons cannot be toggled. Bootstrap Dropdown is built on the third-party library popper which provides dynamic positioning.

How to toggle the dropdown of a list 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. You can also use javascript to toggle the dropdown. So now the dropdown can be toggled.

Why the dropdown does not work?

So if you are using the higher versions of the browsers, then there are chances that the dropdown will not work. So use the latest version of the browsers. Sometimes, we forgot to add data-bs-toggle in the code and then dropdown does not work properly. See the example below.

How do I open a dropdown menu in HTML?

To open the dropdown menu, use a button or a link with a class of .dropdown-toggle and the data-toggle="dropdown" attribute.


2 Answers

From Bootstrap documentation (http://getbootstrap.com/javascript/#dropdowns):

To keep URLs intact with link buttons, use the data-target attribute instead of href="#".

So your code should be

 <a data-target="http://www.google.es"  target="_blank" href="http://www.google.es" class="btn btn-default dropdown-toggle">Lists</a>

<ul class="dropdown-menu" role="menu">
    <li>
        <a href="#">Sub List 1</a>
    </li>
    <li>
        <a href="#">Sub List 2</a>
    </li>
</ul>

Futhermore, you can find more information about how to get menu dropdown using hover instead of click: How to make twitter bootstrap menu dropdown on hover rather than click

like image 157
caballerog Avatar answered Sep 22 '22 23:09

caballerog


A simple jQuery solution:

$('#menu-main > li > .dropdown-toggle').click(function () {
    window.location = $(this).attr('href');
});

But the better solution is to replace the data-toggle attribute with data-hover

So your final code will be:

<div class="btn-group">
   <a href="http://google.com" class="btn btn-default dropdown-toggle" data-hover="dropdown">Lists</a>

    <ul class="dropdown-menu" role="menu">
        <li>
            <a href="#">Sub List 1</a>
        </li>
        <li>
            <a href="#">Sub List 2</a>
        </li>
    </ul>
</div>
like image 24
Allure Web Solutions Avatar answered Sep 22 '22 23:09

Allure Web Solutions