Skip to content

Instantly share code, notes, and snippets.

@MichaelStett
Last active November 3, 2020 10:31
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 MichaelStett/18972b29ce1e651e4a13a63b008aa0ac to your computer and use it in GitHub Desktop.
Save MichaelStett/18972b29ce1e651e4a13a63b008aa0ac to your computer and use it in GitHub Desktop.
Script #1
const Todo = class {
constructor(desc, date) {
this.id = Date.now();
this.desc = desc;
this.date = date;
this.completed = 0;
}
}
let todos = [];
const refreshTodoList = (todos) => {
todoList.innerHTML = '';
for (let todo of todos)
{
const li = document.createElement('li');
li.setAttribute('id', todo.id);
li.innerHTML = `
<div class="input-group mb-3">
<div class="input-group-prepend">
<div class="input-group-text">
<input class="completeTodo" type="checkbox" ${todo.completed == 1 ? 'checked': ''}>
</div>
</div>
<input type="text" minlength="3" maxlength="255" class="form-control" value="${todo.desc}" disabled>
<input type="date" class="form-control" contenteditable="true" disabled value="${todo.date}">
<div class="input-group-append">
<button class="btn btn-danger deleteTodoButton" type="button"><i class="far fa-trash-alt"></i></button>
</div>
</div>
`;
todoList.append(li);
}
console.table(todos);
};
// Load todos from localStorage on page load
const reference = localStorage.getItem('todos');
if (reference) {
todos = JSON.parse(reference);
refreshTodoList(todos);
}
const addToLocalStorage = (todos) => {
localStorage.setItem('todos', JSON.stringify(todos));
refreshTodoList(todos);
}
// Test
todos.push(new Todo("XXX", "2020-11-03"))
addToLocalStorage(todos);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment