One Fiddle is worth a thousand words.
<div id="test-container" class="container-fluid">
<input id="enableButtonCheck" type="checkbox" name="enableButton"/>Enable that button -->
<i id="targetButton"class="disabled btn btn-sm btn-primary">Button</i>
</div>
$('#test-container').on('click', '#targetButton', function(e) {
alert('Hello, I\'m target button.');
})
$('#enableButtonCheck').on('click', function(e) {
$('#targetButton').toggleClass('disabled', !$("#enableButtonCheck").is(":checked"));
})
All works as intended in fiddle. This also used to work in my app before, but after last few commits it stopped. So I guess that it interacts with other parts of my project somehow. I have the same versions of Bootstrap and Jquery, and all other jquery/bootstrap functions work nice.
If I attach handler directly to button it works again.
$('#targetButton').on('click',function(e){
alert('Hello, I\'m target button.');
})
Can somebody give me an idea which kind of interactions can prevent parent event handler from firing?
UPD. It appeared that I have this code which prevents handler from firing. Thanks for suggestions.
$('.disabled').on('click',function clickDisabled(event) {
event.preventDefault();
event.stopPropagation();
})
But I don't understand why, because...
UPD2: I found that I need to use delegated event handler instead of static to prevent events on .disabled class. For instance:
$('.disableds-static-parent').on('click', 'disabled', function clickDisabled(event) {
event.preventDefault();
event.stopPropagation();
})
from this answer.