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.
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 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.
Removing the data attribute data-toggle="dropdown" and implementing the open/close of the dropdown can be a solution. Save this answer.
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.
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(); });
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; });
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