Skip to content

Instantly share code, notes, and snippets.

@mcaskill
Created September 19, 2020 15:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mcaskill/4127278c471271d8a1517bb8a9e0fc75 to your computer and use it in GitHub Desktop.
Save mcaskill/4127278c471271d8a1517bb8a9e0fc75 to your computer and use it in GitHub Desktop.
JS : Persist checked inputs in web storage
(function () {
const table = document.getElementById('my-table');
const store = window.localStorage;
table.querySelectorAll('[type="checkbox"]').forEach(function (input) {
input.checked = !!store.getItem(`${input.name}_${input.value}`);
});
table.addEventListener('change', function (event) {
let input = event.target;
let key = `${input.name}_${input.value}`;
if (input.checked) {
store.setItem(key, new Date());
} else {
store.removeItem(key);
}
let parent = input.parentNode;
while (parent && parent.tagName !== 'TR') {
parent = parent.parentNode;
}
if (parent) {
parent.classList.toggle('tr-done', input.checked);
}
}, {
passive: true
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment