Skip to content

Instantly share code, notes, and snippets.

@lelandbatey
Last active October 30, 2016 23:27
Show Gist options
  • Save lelandbatey/63fa001c4e2eea27f9d3 to your computer and use it in GitHub Desktop.
Save lelandbatey/63fa001c4e2eea27f9d3 to your computer and use it in GitHub Desktop.
Turn html lists into checklists; great for markdown
<script>
var checkboxes = [];
function getCheckboxStates() { return checkboxes.map(bx => !!bx.checked); };
function saveCheckboxStates() {
localStorage['boxStates'] = JSON.stringify(getCheckboxStates());
}
// Add a checkbox in front of every list item, save reference to each
[].slice.call(document.getElementsByTagName('li')).forEach((li, index) => {
var chkbx = document.createElement('input');
chkbx.type = "checkbox";
li.insertBefore(chkbx, li.firstChild);
chkbx.addEventListener('click', saveCheckboxStates);
checkboxes.push(chkbx);
});
// If there are no local-storage states, create them.
if (!localStorage['boxStates']){
saveCheckboxStates();
// Apply the states saved in local-storage
} else {
boxStates = JSON.parse(localStorage['boxStates']);
checkboxes.forEach((chkbx, i) => {chkbx.checked = boxStates[i];});
}
</script>
<style>
ul { list-style-type: none; }
</style>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment