Zidance Zidance - 1 year ago 117
jQuery Question

Why is the bootstrap dropdown not working by using button as option?

<div class="dropdown">
<button class="btn btn-secondary dropdown-toggle" type="button" id="dropdownMenu2" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Dropdown
</button>
<div class="dropdown-menu" aria-labelledby="dropdownMenu2">
<button class="dropdown-item" type="button">Action</button>
<button class="dropdown-item" type="button">Another action</button>
<button class="dropdown-item" type="button">Something else here</button>
</div>
</div>


I want something like dropdown list and the option with button tag. e.g https://www.w3schools.com/bootstrap/tryit.asp?filename=trybs_ref_js_dropdown_js&stacked=h

I want to replace the anchor tag with button, but it will displayed as button shape which i don't want it to be. I tried the code in https://v4-alpha.getbootstrap.com/components/dropdowns/ , but it doesn't displayed as what it looks like. Any way to fix this problem? Thanks.

Answer Source

This will work for you.

I just added some extra CSS custom style to support your buttons, this is needed because as default the style is supported for .dropdown-menu>li>a that means the style will only support the a tag inside the li under the .dropdown-menu, so what I did was - I just styled your button to the default style.

I have also added <span class="caret"></span> to the Dropdown button and also added the class btn-primary use this only if its needed. Hope this helps you.

$(document).ready(function() {
  $(".dropdown-toggle").dropdown();
});
.dropdown-menu>button {
  display: block;
  padding: 3px 20px;
  clear: both;
  font-weight: 400;
  line-height: 1.42857143;
  color: #333;
  white-space: nowrap;
  width: 100%;
  text-align: left;
  background: #fff;
  border: none;
}

.dropdown-menu>button:focus,
.dropdown-menu>button:hover {
  color: #262626;
  text-decoration: none;
  background-color: #f5f5f5;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<div class="dropdown">
  <button class="btn btn-primary dropdown-toggle" type="button" id="dropdownMenu2" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
    Dropdown <span class="caret"></span>
 </button>
  <div class="dropdown-menu" aria-labelledby="dropdownMenu2">
    <button class="dropdown-item" type="button">Action</button>
    <button class="dropdown-item" type="button">Another action</button>
    <button class="dropdown-item" type="button">Something else here</button>
  </div>
</div>

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download