To Do List

In this example below you will see how to do a To Do List with some HTML / CSS and Javascript

Thumbnail
This awesome code was written by 01fade, you can see more from this user in the personal repository.
You can find the original code on Codepen.io
Copyright 01fade ©
  • HTML
  • CSS
  • JavaScript
<!DOCTYPE html>
<html lang="en" >

<head>
  <meta charset="UTF-8">
  <title>To Do List</title>
  
  
  
      <link rel="stylesheet" href="css/style.css">

  
</head>

<body>

  <div id="toDoList">
		<h2>To Do List</h2>
		<input type="text" id="newTask">
		<input type="submit" id="addButton" value="Add task">
		<div id="taskList"></div>
</div>
  
  

    <script  src="js/index.js"></script>




</body>

</html>

/*Downloaded from https://www.codeseek.co/01fade/to-do-list-YXMGQy */
.task {
	background-color: #cccccc;
}

/*Downloaded from https://www.codeseek.co/01fade/to-do-list-YXMGQy */
var taskArray = [];

function deleteAllTask() {
		taskArray = [];
		updateTasks();
};

function addDeleteAllButton(taskListDiv) {
		if (taskArray.length > 0) {
				var deleteAll = document.createElement("button");
				deleteAll.innerText = "delete all";
				deleteAll.id = "deleteAll";
				deleteAll.addEventListener("click", function(event) {
						event.preventDefault();
						deleteAllTask();
				});
				taskListDiv.appendChild(deleteAll);
		};
};

function deleteTask(event) {
	var taskIndex = event.target.parentElement.id;
	taskArray.splice(taskIndex, 1);
	updateTasks();
};

function addDeleteButton (newItem, taskListDiv) {
	var deleteButton = document.createElement("button");
	deleteButton.innerText = "Delete";
	deleteButton.id = "deleteButton";
	deleteButton.addEventListener("click", function(event){
		event.preventDefault();
		// console.log(event);
		deleteTask(event);
	});
	newItem.appendChild(deleteButton);
	taskListDiv.appendChild(newItem);
};

function updateTasks(){
	var taskListDiv = document.getElementById("taskList");
	taskListDiv.innerHTML = "";
	for (var i = 0; i<taskArray.length; i++){
		var newItem = document.createElement("div");
		newItem.id = i;
		newItem.className = "task";
		var newItemP = document.createElement("p");
		newItemP.innerText = taskArray[i];
		newItem.appendChild(newItemP);
		addDeleteButton(newItem, taskListDiv);
	};
	addDeleteAllButton(taskListDiv);
};

function saveTask(){
	var newTask = document.getElementById("newTask");
	var taskValue = newTask.value;
	newTask.value = "";
	taskArray.push(taskValue);
	updateTasks();
	console.log(taskArray);
};

function initialize () {
	var addButton = document.getElementById("addButton");
	addButton.addEventListener("click", function(event){
// 		event.preventDefault();
		// console.log(event);
		saveTask();
	});
};

window.onload = initialize();

Comments