Skip to content

Instantly share code, notes, and snippets.

@rungta
Last active February 14, 2020 14:28
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Embed
What would you like to do?
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