Skip to content

Instantly share code, notes, and snippets.

@sergey-shambir
Created July 5, 2019 03:58
Show Gist options
  • Save sergey-shambir/f99faea0649361d939167f974945c5b0 to your computer and use it in GitHub Desktop.
Save sergey-shambir/f99faea0649361d939167f974945c5b0 to your computer and use it in GitHub Desktop.
To-Do App JS, v1
function onPageLoaded() {
const input = document.querySelector("input[type='text']");
const ul = document.querySelector("ul.todos");
function createTodo() {
const li = document.createElement("li");
const textSpan = document.createElement("span");
textSpan.classList.add("todo-text");
const newTodo = input.value;
textSpan.append(newTodo);
const deleteBtn = document.createElement("span");
deleteBtn.classList.add("todo-trash");
const icon = document.createElement("i");
icon.classList.add("fas", "fa-trash-alt");
deleteBtn.appendChild(icon);
ul.appendChild(li).append(textSpan, deleteBtn);
input.value = "";
listenDeleteTodo(deleteBtn);
}
input.addEventListener("keypress", (keyPressed) => {
const keyEnter = 13;
if (keyPressed.which == keyEnter) {
createTodo();
}
});
ul.addEventListener("click", onClickTodo);
}
document.addEventListener("DOMContentLoaded", onPageLoaded);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment