I built a small "todo" app with an object oriented JavaScript approach.
I have spent this morning reading on a few ways to approach this, and I've come up with the following:
document.addEventListener("DOMContentLoaded", function () {
var todo = Object.create(null);
todo.newTodo = function (evt) {
evt.preventDefault();
var newTodo = document.getElementById("todo-item").value;
var ul = document.getElementById("todo-view");
var li = document.createElement("li");
li.innerHTML = newTodo + '<span>x</span>';
//using tertnary operator to add items to top of list if other items already exist
(ul.childElementCount == 0) ? ul.appendChild(li) : ul.insertBefore(li, ul.firstChild);
document.getElementById('todo-item'). value = "";
var span = li.getElementsByTagName("span");
span[0].addEventListener("click", todo.deleteTodo, false);
}
var todoCache = [];
todo.deleteTodo = function () {
todoCache.push(this.parentNode);
this.parentNode.parentNode.removeChild(this.parentNode);
return todoCache;
}
todo.undoDelete = function () {
var ul = document.getElementById("todo-view");
if (todoCache.length > 0 ) {
var lastTodo = todoCache.length - 1;
ul.appendChild(todoCache[lastTodo]);
todoCache.pop();
}
}
var todoSub = document.getElementById("submit-item");
todoSub.addEventListener("click", todo.newTodo, false);
var undo = document.getElementById('undo');
undo.addEventListener("click", todo.undoDelete, false);
});
body, html {
width: 100%;
height: 100%;
margin: 0;
padding: 0;
}
body {
background: #ccc;
font-family: 'Open Sans Condensed', sans-serif;
font-weight: 100;
}
.list-container {
padding: 15px 10px;
margin: 0 auto;
max-width: 30em;
text-align: center;
}
.input {
width: 100%;
}
.input input {
height: 50px;
display: block;
width: 100%;
margin: 0 auto;
box-sizing: border-box;
transition: linear .2s;
}
input[type="text"] {
padding: 10px 20px;
margin: 10px auto;
border: none;
}
input[type="submit"] {
background: #7FFFD4;
border: none;
color: white;
padding: 0;
display: block;
cursor: pointer;
font-size: 15px;
text-transform: uppercase;
}
input[type="submit"]:hover {
background-color: #66ccaa;
}
h1 {
text-transform: uppercase;
font-size: 40px;
color: #fff;
}
ul {
margin: 0;
padding: 0;
}
li {
list-style-type: none;
padding: 7px 0;
width: 100%;
transition: .2s linear;
position: relative;
}
li:hover span {
display: inline-block;
}
li span {
position: absolute;
right: 10px;
color: red;
margin-right: 5px;
display: none;
cursor: pointer;
font-size: 20px;
}
a {
color: white;
}
<html>
<head>
<meta charset="UTF-8">
</head>
<link href='https://fonts.googleapis.com/css?family=Open+Sans+Condensed:300' rel='stylesheet' type='text/css'>
<body>
<div class="list-container">
<h1>TODO List</h1>
<a id="undo" href="javascript:;">UNDO</a>
<ul id="todo-view">
</ul>
<div class="input">
<form>
<input id="todo-item" type="text" placeholder="Enter your tasks here...">
<input id="submit-item" type="submit" value="add">
</form>
</div>
</div>
</body>
</html>
What do you suggest i do to improve this? Is there anything else you recommend to help further my learning efforts?
span[0].addEventListener("click", todo.deleteTodo, false);
thetodo.deleteTodo
will break ifdeleteTodo
usesthis
. – gcampbell Jun 28 at 17:31span[0].addEventListener("click", function () { todo.deleteTodo() })
. – gcampbell 2 days agothis
rules. – gcampbell 2 days ago