Skip to content

Instantly share code, notes, and snippets.

@rungta
Last active February 14, 2020 14:28
Show Gist options
  • Save rungta/ee5fbd5a780d804646fc52846e04643a to your computer and use it in GitHub Desktop.
Save rungta/ee5fbd5a780d804646fc52846e04643a to your computer and use it in GitHub Desktop.
Uncheck input.checkbox-scope-control[type=checkbox] on a click outside .checkbox-scope
(function (w) {
function closeScopedCheckboxes(checkbox_selector, scope_selector) {
return function (event) {
var el = event.target;
document
.querySelectorAll(scope_selector)
.forEach(function(toggleScope) {
if (!toggleScope.contains(el)) {
Array
.from(toggleScope.querySelectorAll(checkbox_selector))
.filter(function (checkbox) {
return checkbox.checked && !(el instanceof HTMLLabelElement && checkbox == el.control);
})
.map(function (checkbox) {
checkbox.checked = false;
});
}
});
};
}
document.addEventListener('click', closeScopedCheckboxes('.checkbox-scope-control', '.checkbox-scope'));
document.addEventListener('touchend', closeScopedCheckboxes('.checkbox-scope-control', '.checkbox-scope'));
}(this));
{
"name": "checkboxscope",
"version": "0.1.0"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment