I am trying to add a bootstrap dropdown inside another clickable parent. It's kind of tricky I guess. Here is the Codepen I created with the issue recreated.
Codepen with the issue recreated
Expected behavior: On clicking the dropdown, the dropdown will fire, so will by clicking the other button (optional if too tricky) and none will trigger a click on the parent.
<div class="card-body parent" style="min-height: 300px;">
<!-- ORIGINAL DROPPER BUTTON -->
<div class="dropdown original-dropper mb-5">
<button class="btn btn-secondary dropdown-toggle" type="button" id="dropdownMenuButton" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Dropdown button
</button>
<div class="dropdown-menu" aria-labelledby="dropdownMenuButton">
<a class="dropdown-item" href="#">Action</a>
<a class="dropdown-item" href="#">Another action</a>
</div>
</div>
<!-- OTHER DROPPER BUTTON -->
<button class="btn btn-danger other-dropper mt-5 d-inline">Other Button</button>
</div>
JS:
$('.parent').on('click', function(e) {
$(this).css('backgroundColor', 'darkorange');
});
$('.other-dropper').on('click', function(e) {
e.stopPropagation();
$('.original-dropper .dropdown-toggle').dropdown();
console.log('clicked');
});
dropdown(), that will create dropdown menu, you need to add classes to open dropdown, The easiest way is open dev tools open dropdown and see what changing (show class are added to two elements) so you need to add same classes with jQuery. Best is to use.toggleClass('show')