Last active
October 30, 2016 23:27
-
-
Save lelandbatey/63fa001c4e2eea27f9d3 to your computer and use it in GitHub Desktop.
Turn html lists into checklists; great for markdown
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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