Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bootstrap dropdown closing when clicked

I put a form inside a bootstrap dropdown, but when I click any of the fields in the form, the dropdown goes away. I have this piece of code but I don't know where to put it to prevent the dropdown disappearing.

$(function test() {   // Setup drop down menu   $('.dropdown-toggle').dropdown();    // Fix input element click problem   $('.dropdown input, .dropdown label').click(function(e) {     e.stopPropagation();   }); }); 

Could someone please tell me where I should put this? I tried in the bootstrap-dropdown, I tried within the HTML but it still doesn't work.

like image 651
Sorin Cioban Avatar asked Jun 02 '12 15:06

Sorin Cioban


People also ask

How do I stop a dropdown from closing on click?

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

How do I make a dropdown stay open?

Just wrap the contents of the menu in a form tag. That's it. So, instead of ul. dropdown-menu , use form.

Why is my dropdown menu not working 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 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.


1 Answers

You can omit the dropdown call all together, the bootstrap dropdown works without it. Make sure you're wrapping your script properly, you can include it on the header or body of your page, although personally i prefer placing it on the body of your page.

JS

<script type="text/javascript">     $('.dropdown-menu input, .dropdown-menu label').click(function(e) {         e.stopPropagation();     }); </script> 
like image 124
Andres Ilich Avatar answered Sep 19 '22 15:09

Andres Ilich