Select Git revision
script.js 1.40 KiB
indexNum = 0;
addBtn = document.getElementById("add-task");
addBtn.onclick = addTask;
taskInp = document.getElementById("task-input");
taskInp.addEventListener("keyup",function(event){
// If enter is pressed
if (event.keyCode == 13) {
addTask();
}
});
function addTask() {
taskInput = document.getElementById("task-input").value;
taskList = document.getElementById("tasks");
taskLi = document.createElement("li");
taskLi.setAttribute("class", "task");
taskCheckbox = document.createElement("input");
taskCheckbox.setAttribute("type", "checkbox");
taskCheckbox.setAttribute("id", "task" + indexNum);
taskVal = document.createElement("label");
taskVal.setAttribute("for", "task" + indexNum);
taskVal.innerText = taskInput;
taskBtn = document.createElement("button");
taskBtn.setAttribute("id","bin" + indexNum);
taskBtn.onclick = function(){
removeTask(this.id);
}
trashIcon = document.createElement("i");
trashIcon.setAttribute("class", "fas fa-trash-alt");
taskBtn.appendChild(trashIcon);
taskLi.appendChild(taskCheckbox);
taskLi.appendChild(taskVal);
taskLi.appendChild(taskBtn);
taskList.appendChild(taskLi);
document.getElementById("task-input").value = "";
indexNum++;
}
function removeTask(id){
taskList.removeChild(document.getElementById(id).parentElement);
}