Skip to content

Instantly share code, notes, and snippets.

@mathieutu
Created September 6, 2017 16:48
Show Gist options
  • Save mathieutu/32876fdfbc5d6c4a691a08cc2e46fe43 to your computer and use it in GitHub Desktop.
Save mathieutu/32876fdfbc5d6c4a691a08cc2e46fe43 to your computer and use it in GitHub Desktop.
How to make parent/child checkboxes.
document.querySelector('form').addEventListener('input', (event) => {
let match;
if (match = /child-(\d+)/.exec(event.target.id) && event.target.checked) {
document.querySelector(`#parent-${match[1]}`).checked = true;
}
if (match = /parent-(\d+)/.exec(event.target.id) && !event.target.checked) {
document.querySelector(`#child-${match[1]}`).checked = false;
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment