7

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>
madth3
  • 7,275
  • 12
  • 50
  • 74
srt8driver
  • 481
  • 4
  • 16

2 Answers2

18

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>
srt8driver
  • 481
  • 4
  • 16
3

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.

Astockwell
  • 1,498
  • 13
  • 11
  • This is the correct answer. [data-toggle="collapse"] can be replaced with the specific class of the menu AND 'body' (the dropdown also closes when you click anywhere within the BODY). e.g 'main_menu, body' – Steven Jun 03 '15 at 12:14