1

Would like to ask I have a simple application in the navbar which consist a button like below snippet.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>

<nav class="navbar navbar-default">
  <div class="container-fluid">
    <!-- Collect the nav links, forms, and other content for toggling -->
    <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
      <ul class="nav navbar-nav">
        <li class="dropdown">
          <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">This should close when click <span class="caret"></span></a>
          <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>
            <li role="separator" class="divider"></li>
            <li><a href="#">Separated link</a>
            </li>
            <li role="separator" class="divider"></li>
            <li><a href="#">One more separated link</a>
            </li>
          </ul>
        </li>
        <li class="dropdown">
          <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Do not close when button click <span class="caret"></span></a>
          <ul class="dropdown-menu">
            <li style="text-align:center">
              <button class="btn btn-default" type="submit">Button</button>
            </li>
          </ul>
        </li>
      </ul>
    </div>
    <!-- /.navbar-collapse -->
  </div>
  <!-- /.container-fluid -->
</nav>

How should I code it in such a way that when the button in the dropdown menu is clicked and it should not close the dropdown? Also the css should only appect that particular dropdown menu and not the others?

Note : please run code in full page.

Kenny Yap
  • 1,317
  • 5
  • 17
  • 39

1 Answers1

2

I gave the ul element the id "dropstop" ,and applied this following jquery code:

$('#dropstop').click(function(event){
     event.stopPropagation();
 });

This is the JSfiddle for it.

I found the jquery code from this post : Stack Overflow Post

Community
  • 1
  • 1
BahaEddine Ayadi
  • 987
  • 9
  • 20