From a4ae6900a92daab7159f34855849768641de0d8d Mon Sep 17 00:00:00 2001 From: Drew Miller Date: Tue, 28 Feb 2017 14:35:17 -0800 Subject: [PATCH] Activity 1 --- src/index.js | 45 ++++++++++++++++++++++++++++++++++++--------- static/index.html | 1 - 2 files changed, 36 insertions(+), 10 deletions(-) diff --git a/src/index.js b/src/index.js index 30e1a0f..725771f 100644 --- a/src/index.js +++ b/src/index.js @@ -1,4 +1,24 @@ -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' ); @@ -6,22 +26,30 @@ function onReady () { // 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 //
  • {title}
  • 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 = ''; }); @@ -32,4 +60,3 @@ if ( document.readyState !== 'loading' ) { } else { document.addEventListener( 'DOMContentLoaded', onReady ); } - diff --git a/static/index.html b/static/index.html index 425855a..f397d8c 100644 --- a/static/index.html +++ b/static/index.html @@ -32,4 +32,3 @@

    The Todo App!

    -