Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 36 additions & 9 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,55 @@
function onReady () {
let todos = [
// your initial todos
{ title: 'Dishes', complete: false },
{ title: 'Laundry', complete: false },
{ title: 'Vacuuming', complete: false},
{ title: 'Making the bed', complete: false},
{ title: 'Budgeting', complete: false}
];

function createNewTodo ( title ) {
todos.push({
title,
complete: false,
});

// The "state" changed, so re-draw the UI
renderTheUi( todos );
}

function renderTheUi ( todos ) {
// TODO: using the todos, draw out the UI
const addTodoForm = document.getElementById( 'addTodoForm' );
const todoList = document.getElementById( 'todoList' );
const newTodoText = document.getElementById( 'newTodoText' );

// <ul></ul>
todoList.textContent = '';

addTodoForm.addEventListener( 'submit', event => {
event.preventDefault();
const title = newTodoText.value;

for (let x = 0; x < todos.length; x++){
// add it to the list
// create an li
// <li>{title}</li>
const newLi = document.createElement( 'li' );
newLi.textContent = title; // vs newLi.innerHTML
newLi.textContent = todos[x].title; // vs newLi.innerHTML

newLi.addEventListener( 'click', () => {
newLi.classList.toggle( 'todo--complete' );
});

// put it in the ul
todoList.appendChild( newLi );
}
}

function onReady () {

// Draw the UI the first time
renderTheUi( todos );

addTodoForm.addEventListener( 'submit', event => {
event.preventDefault(); //keeps page from refreshing
const title = newTodoText.value;

createNewTodo(title);

newTodoText.value = '';
});
Expand All @@ -32,4 +60,3 @@ if ( document.readyState !== 'loading' ) {
} else {
document.addEventListener( 'DOMContentLoaded', onReady );
}

1 change: 0 additions & 1 deletion static/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,3 @@ <h1>The Todo App!</h1>
<script src="/static/bundle.js"></script>
</body>
</html>