// namespace to avoid globals
var myApp = myApp || {
checkBoxes: {},
originalText: "",
toggleText: "Checked ",
handleCheckboxEvents: function(event) {
// keep event from bubble to 'a' tag
event.stopImmediatePropagation()
let checkedOnes = myApp.checkBoxes.filter(':checked');
let checkCount = checkedOnes.length;
let checkedAll = checkCount === myApp.checkBoxes.length;
// use if single checked for label
let label = checkedOnes.first()
.closest('.dropdown-item')
.find('.check-label').html();
// labels can come from data also like this example
let checkallLabel = $('.checkall-item').data('all');
// non checked use original
let selText = checkCount === 0 ?
myApp.originalText :
// one checked use its label
checkCount === 1 ? label :
// checked all?
checkedAll ? checkallLabel :
// more than one - use number checked
myApp.toggleText + checkCount;
// set the text we determined
$(this).closest('.btn-group')
.find('.dropdown-toggle')
.html(selText);
$('#checkall').trigger('set-checkall', [{
source: 'checkbox'
}]);
},
handleLinkClick: function(event) {
// keep link from being followed
event.preventDefault();
// keep dropdown menu from closing
event.stopPropagation();
let cb = $(this).find('input[type="checkbox"]');
let isChecked = cb.prop("checked");
cb.prop("checked", !isChecked);
cb.trigger('change');
},
handleCheckAll: function(event, data) {
// use the source of the event to set it
if (data.source == 'checkall') {
// toggle to what it is not
$(this).prop('checked', !this.checked);
$('.cb-element').prop('checked', this.checked).trigger('change');
}
if (data.source === 'checkbox') {
// set depending on how many checked
$(this).prop('checked', myApp.checkBoxes.filter(':checked').length == myApp.checkBoxes.length);
}
},
// initialize and attach event handlers
init: function() {
this.checkBoxes = $('.cb-element');
this.originalText = $('.btn-secondary.dropdown-toggle').html();
// handle the checkbox click or change
$(".dropdown-menu").find('a.dropdown-item')
.on('click change', 'input[type="checkbox"]', this.handleCheckboxEvents);
$(".dropdown-menu").find('a.dropdown-item')
.on('click', this.handleLinkClick);
// handle the checkall change by its wrapper click
$('.checkall-item').on('click', function() {
event.stopPropagation();
$('#checkall').trigger('set-checkall', [{
source: 'checkall'
}]);
});
// use custom event so we can use multiple places, passing it data
$('#checkall').on('set-checkall', this.handleCheckAll);
}
};
myApp.init();
.checkall-item .check-label {
margin-bottom: 1em;
border-bottom: 1px green solid;
display: inline-block;
font-weight:bold;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
<div class="btn-group my-dropdown-group">
<button class="btn btn-secondary dropdown-toggle text-center" type="button" data-toggle="dropdown">Dropdown button</button>
<div class="dropdown-menu">
<a class="dropdown-item checkall-item" data-all="Checked All"><input type="checkbox" name="all" class="mr-2" id="checkall"><span class="check-label">Check All</span></a>
<a class="dropdown-item"><input type="checkbox" class="cb-element mr-2"><span class="check-label">Checkbox Fun</span></a>
<a class="dropdown-item"><input type="checkbox" class="cb-element mr-2"><span class="check-label">Checkbox Happy</span></a>
<a class="dropdown-item"><input type="checkbox" class="cb-element mr-2"><span class="check-label">Checkbox Now</span></a>
</div>
</div>