Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Twitter Bootstrap cant stop a dropdown from closing on click

I have looked at the other questions similar and tried many of the solutions, but none have worked. Here is the issue, this is a mini login form for the top banner. I need the button to not automatically close when you click in the fields. Here is my code:

    <script> 
    $(document).on('click', '.dropdown-menu', function(e){
        $(this).hasClass('keep_open') && e.stopPropagation(); // This replace if conditional.
    });
     ​</script>                 
    <div class="btn-group" >
      <a class="btn btn-small btn-inverse dropdown-toggle" data-toggle="dropdown" href="#">
        <i class="cus-key"></i> Login
        <span class="caret"></span>
      </a>
      <ul class="dropdown-menu pull-right keep_open">
        <form action="clog.php" method="post" class="keep_open">
        <!-- dropdown menu links -->
          <li><input type="text" placeholder="Username..." class="keep_open" /></li>
          <li><input type="text" placeholder="Password..." class="keep_open"/></li>
          <li><input type="submit" name="submit" style="background-image: url('img/login.png'); width: 110px; height: 32px; cursor: hand; margin-top: -5px" value=" " /></li>
          <li><a href="/riders/register.php" ><span style="color: green; float: right" > Sign up for account<i class="icon-double-angle-right"></i></a></span></li>
         </form>
      </ul>
    </div>
like image 533
srt8driver Avatar asked Aug 19 '13 04:08

srt8driver


People also ask

How do I stop bootstrap dropdown from closing?

This behavior can be changed by using the autoClose property. By default, autoClose is set to the default value true and behaves like expected. By choosing false, the dropdown menu can only be toggled by clicking on the dropdown button.

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. This solution is working fine.

How do I stop dropdown menu to close menu items on clicking outside?

Answer: Use the jQuery on() method You can use the jQuery click() method in combination with the on() method to hide the dropdown menu when the user click outside of the trigger element.

How do I keep my drop down menus open?

Set toggle={false} property inside DropdownItem to prevent dropdown from closing.


2 Answers

I figured it out. It was not inside the document ready function. (hat tip to Koala_dev) Javascript needs to be:

    <script type="text/javascript">

    $(document).ready(function() {
        $(document).on('click', '.dropdown-menu', function (e) {
            $(this).hasClass('keep_open') && e.stopPropagation(); // This replace if conditional.
        }); 
    });
    </script>
like image 118
srt8driver Avatar answered Oct 30 '22 01:10

srt8driver


I was having the same problem with an accordion/toggle sub-menu that was nested within a dropdown-menu in Bootstrap 3. Borrowed this syntax from the source code to stop all collapse toggles from closing the dropdown:

$(document).on(
    'click.bs.dropdown.data-api', 
    '[data-toggle="collapse"]', 
    function (e) { e.stopPropagation() }
);

You can replace [data-toggle="collapse"] with whatever you want to stop closing the form, e.g. another class or another data property.

like image 45
Astockwell Avatar answered Oct 30 '22 02:10

Astockwell