Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Allow click on twitter bootstrap dropdown toggle link?

We have setup the twitter bootstrap dropdown to work on hover (as opposed to click [yes we are aware of the no hover on touch devices]). But we want to be able to have the main link work when we click it.

By default twitter bootstrap blocks it, so how can we re-enable it?

like image 202
Hailwood Avatar asked Sep 27 '12 21:09

Hailwood


People also ask

How do you open Bootstrap dropdown menu on hover rather than click?

Answer: Use the jQuery hover() method By default, to open or display the dropdown menu in Bootstrap you have to click on the trigger element. However, if you want to show the dropdown on mouseover instead of click you can do it with little customization using the CSS and jQuery.

How do I toggle a drop down menu?

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.

Why dropdown link is not working in Bootstrap?

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 do I stop dropdown menu Close menu items on clicking inside?

As all Twitter Bootstrap users know, the dropdown menu closes on click (even clicking inside it). To avoid this, I can easily attach a click event handler on the dropdown menu and simply add the famous event. stopPropagation() .


2 Answers

Just add disabled as a class on your anchor:

<a class="dropdown-toggle disabled" href="http://google.com">     Dropdown <b class="caret"></b></a> 

So all together something like:

<ul class="nav">     <li class="dropdown">         <a class="dropdown-toggle disabled" href="http://google.com">             Dropdown <b class="caret"></b>         </a>         <ul class="dropdown-menu">             <li><a href="#">Link 1</a></li>             <li><a href="#">Link 2</a></li>         </ul>     </li> </ul> 
like image 134
David Spence Avatar answered Oct 02 '22 20:10

David Spence


Since there is not really an answer that works (selected answer disables dropdown), or overrides using javascript, here goes.

This is all html and css fix (uses two <a> tags):

<ul class="nav">  <li class="dropdown dropdown-li">     <a class="dropdown-link" href="http://google.com">Dropdown</a>     <a class="dropdown-caret dropdown-toggle"><b class="caret"></b></a>     <ul class="dropdown-menu">         <li><a href="#">Link 1</a></li>         <li><a href="#">Link 2</a></li>     </ul>  </li> </ul> 

Now here's the CSS you need.

.dropdown-li {     display:inline-block !important; } .dropdown-link {     display:inline-block !important;      padding-right:4px !important; } .dropdown-caret {     display:inline-block !important;      padding-left:4px !important; } 

Assuming you will want the both <a> tags to highlight on hover of either one, you will also need to override bootstrap, you might play around with the following:

.nav > li:hover {     background-color: #f67a47; /*hover background color*/ } .nav > li:hover > a {     color: white; /*hover text color*/ } .nav > li:hover > ul > a {     color: black; /*dropdown item text color*/ } 
like image 27
kingPuppy Avatar answered Oct 02 '22 20:10

kingPuppy