Skip to content

Instantly share code, notes, and snippets.

@benekastah
Last active August 29, 2015 14:25
Show Gist options
  • Save benekastah/0d31b2a8c6f6f398fa67 to your computer and use it in GitHub Desktop.
Save benekastah/0d31b2a8c6f6f398fa67 to your computer and use it in GitHub Desktop.
<!doctype html>
<html>
<head>
<title>Todo App</title>
<script type="text/javascript">
var todos = [];
function Todo(title, description, done) {
this.id = null;
this.title = title;
this.description = description;
this.done = done;
}
var createTodo = function () {
return new Todo(prompt('Todo title'), prompt('Todo description'), false);
};
var addTodo = function (todo) {
todo.id = todos.push(todo);
renderTodo(todo);
};
var renderTodo = function (list, todo) {
todo.id = todos.push(todo);
var ul = document.getElementById('todos');
var li = document.createElement('li');
li.setAttribute('id', 'todo-' + todo.id);
li.innerHTML = '<h3>' + todo.title + '</h3>' +
'<p>' + todo.description + '</p>' +
'<input type="checkbox" checked="' + (todo.done ? 'checked' : '') +
'onclick="todo.done = !todo.done; renderTodo(todo)" />';
ul.appendChild(li);
};
addTodo(new Todo('Create todos', 'Create more todos for yourself!', false));
</script>
</head>
<body>
<h1>Todo list</h1>
<ul id="todos"></ul>
<button id="add-todo" onclick="addTodo(createTodo())">Add todo</button>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment