Last active
October 22, 2020 03:53
-
-
Save mrcampbell/340f314b31020f9d1480040aa1dd2d36 to your computer and use it in GitHub Desktop.
This file contains 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
<!DOCTYPE html> | |
<html> | |
<ul id="todo-list"> | |
</ul> | |
// [{"name":"Hit the Gym","is_completed":false},{"name":"Go to the store","is_completed":true}, {"name": "get guhd", "is_completed": true}] | |
<script> | |
const LOCAL_STORAGE_TODO_LIST = "LOCAL_STORAGE_TODO_LIST"; | |
const listElement = document.getElementById("todo-list"); | |
function saveList(list) { | |
localStorage.setItem(LOCAL_STORAGE_TODO_LIST, JSON.stringify(list)); | |
} | |
function readList() { | |
try { | |
let value = localStorage.getItem(LOCAL_STORAGE_TODO_LIST); | |
let data = JSON.parse(value); | |
return data; | |
} catch (err) { | |
alert(err); | |
} | |
} | |
function clearList() { | |
localStorage.removeItem(LOCAL_STORAGE_TODO_LIST); | |
} | |
function addTodoToList(todo) { | |
let todos = readList(); | |
todos.append(todo); | |
saveList(todos); | |
} | |
function populateListElement() { | |
let todos = readList(); | |
todos.forEach((todo) => { | |
let todoElement = document.createElement("li"); | |
let checkmark = ' '; | |
if (todo.is_completed) { | |
checkmark = 'X' | |
} | |
todoElement.appendChild(document.createTextNode(todo.name + ': [' + checkmark + ']')); | |
listElement.appendChild(todoElement) | |
}); | |
} | |
function main() { | |
populateListElement(); | |
} | |
main() | |
</script> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment