Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Event handlers for Twitter Bootstrap dropdowns?

I'd like to use a Twitter Bootstrap dropdown button:

    <div class="btn-group">       <button class="btn dropdown-toggle" data-toggle="dropdown">Action <span class="caret"></span></button>       <ul class="dropdown-menu">         <li><a href="#">Action</a></li>         <li><a href="#">Another action</a></li>         <li><a href="#">Something else here</a></li>       </ul>     </div><!-- /btn-group --> 

When the user changes the value of the button to 'Another action', instead of simply navigating to #, I'd actually like to handle the action in a custom way.

But I can't see any event handlers in the dropdown plugin for doing this.

Is it actually possible to use Bootstrap dropdown buttons to handle user actions? I'm starting to wonder if they're only intended for navigation - it's confusing that the example gives a list of actions.

like image 619
Richard Avatar asked Aug 16 '12 14:08

Richard


People also ask

How do I add a drop down menu in 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.

Why is my dropdown menu not working Bootstrap?

Why is my drop down menu 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?

Removing the data attribute data-toggle="dropdown" and implementing the open/close of the dropdown can be a solution. Save this answer.

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.


2 Answers

Twitter bootstrap is meant to give a baseline functionality, and provides only basic javascript plugins that do something on screen. Any additional content or functionality, you'll have to do yourself.

<div class="btn-group">   <button class="btn dropdown-toggle" data-toggle="dropdown">Action <span class="caret"></span></button>   <ul class="dropdown-menu">     <li><a href="#" id="action-1">Action</a></li>     <li><a href="#" id="action-2">Another action</a></li>     <li><a href="#" id="action-3">Something else here</a></li>   </ul> </div><!-- /btn-group --> 

and then with jQuery

jQuery("#action-1").click(function(e){ //do something e.preventDefault(); }); 
like image 145
Thomas Jones Avatar answered Sep 16 '22 20:09

Thomas Jones


Try this:

$('div.btn-group ul.dropdown-menu li a').click(function (e) {     var $div = $(this).parent().parent().parent();      var $btn = $div.find('button');     $btn.html($(this).text() + ' <span class="caret"></span>');     $div.removeClass('open');     e.preventDefault();     return false; }); 
like image 21
Fernando Avatar answered Sep 19 '22 20:09

Fernando