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>
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.
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.
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.
To open the dropdown menu, use a button or a link with a class of .dropdown-toggle and the data-toggle="dropdown" attribute.
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
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With