bookwyrm/bookwyrm/static/js/check_all.js
2021-04-06 09:11:56 +02:00

25 lines
732 B
JavaScript

/**
* Toggle all descendant checkboxes of a target.
*
* Use `data-target="ID_OF_TARGET"` on the node on which the event is listened
* to (checkbox, button, link…), where_ID_OF_TARGET_ should be the ID of an
* ancestor for the checkboxes.
*/
(function() {
'use strict';
function toggleAllCheckboxes(event) {
const mainCheckbox = event.target;
document
.querySelectorAll(`#${mainCheckbox.dataset.target} [type="checkbox"]`)
.forEach(checkbox => checkbox.checked = mainCheckbox.checked);
}
document
.querySelectorAll('[data-action="toggle-all"]')
.forEach(input => {
input.addEventListener('change', toggleAllCheckboxes);
});
})();