Skip to content

Instantly share code, notes, and snippets.

@yetanothernguyen
Last active August 29, 2015 14:01
Show Gist options
  • Save yetanothernguyen/e29815b0f40778a55d5a to your computer and use it in GitHub Desktop.
Save yetanothernguyen/e29815b0f40778a55d5a to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<title>Todo</title>
<link rel="stylesheet" type="text/css" href="todo.css">
<script type="text/javascript">
window.addEventListener('load', run);
function run() {
var todoInput = document.getElementById("todo-input");
var todoContainer = document.getElementById("todoapp");
todoContainer.addEventListener('click', doneCheckboxHandler);
todoInput.addEventListener("keyup", inputHandler);
}
function inputHandler(event) {
var keycode = event.keyCode;
if (keycode == 13) {
var inputElement = event.target;
var inputValue = inputElement.value;
if (inputValue.length > 0) {
var todoList = document.getElementById("todo-list");
todoList.innerHTML += "<li><input type='checkbox' class='done-checkbox'><label>" + inputValue + "</label></li>";
inputElement.value = "";
}
}
}
function doneCheckboxHandler(event) {
if (event.target.className == "done-checkbox") {
var liElement = event.target.parentNode;
if (liElement.className == "done") {
liElement.className = "";
} else {
liElement.className = "done";
}
}
}
</script>
</head>
<body>
<div id="todoapp">
<h1>Todos</h1>
<input type="text" size="30" id="todo-input" placeholder="What do you need to do" autofocus/>
<ul id="todo-list">
</ul>
</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment