Problem
Event handler not called when button inside Bootstrap dropdown.
What I want
When the dropdown button with id repeat_booking_button is clicked, I want the event handler to be called.
What i've tried
I've moved the button outside of the dropdown and the event handler is called as expected 'Scenario 1'.
Scenario 1 [Working]
<div class="button_wrapper">
<button type="button" id="repeat_booking_button">Repeat Booking</button>
<div class="dropdown">
<button class="btn dark_grey_button dropdown-toggle" type="button" id="dropdownMenuButton" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Options
</button>
<div class="dropdown-menu" aria-labelledby="dropdownMenuButton">
</div>
</div>
</div>
Scenario 2 [Not Working]
<div class="button_wrapper">
<div class="dropdown">
<button class="btn dark_grey_button dropdown-toggle" type="button" id="dropdownMenuButton" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Options
</button>
<div class="dropdown-menu" aria-labelledby="dropdownMenuButton">
<button type="button" class="dropdown-item" id="repeat_booking_button">Repeat Booking</button>
</div>
</div>
</div>
Javascript
$(document).on('click', '#repeat_booking_button', function() {
$("#repeat_booking_overlay").show();
$(".page_mask_allocation").show();
});
I'm guessing having an id attribute inside the dropdown conflicts with the functionality of the dropdown?
[UPDATE]
In a included custom javascript file I found the following function which was contributing to the error. Removed and the system works as expected. :
$('body').on("click", ".dropdown-menu", function (e) {
$(this).parent().is(".show") && e.stopPropagation();
});
I'd like to keep this included as it keeps the dropdown open when the mouse moves away from the dropdown. Is there any solution to this?
.page_mask_allocation) and an ID (#repeat_booking_overlay) that are not part of your example. Maybe there's some issue with the CSS of those sections or their containing elements?e.stopPropagation(). This is preventing any further JS from being executed. I can't say why that's there since I'm not seeing the whole webpage, or if it can be safely removed, but that's the source of your other event handler not triggering. You may need to combine these functions ife.stopPropagation()cannot be removed.