Javascript Demo
Now that we a UI that looks like a todo app, we need to add functionality to make it function like a todo app. In this example we are going to use raw Javascript explicitly modify our application as we interact with it. This will be in stark contrast to the implicit approach we will take when we do this with React in the next exercise.
Keep an eye on how often user actions directly modify the HTML on the page. You'll see this number drop to zero when we start using React.
Demo
This demo starts off with a few elements already in place. Let's walk through what's already here.
- clearInput() - This is a generic, reusable function that takes in a
selectorparamater, finds the first matching element, and sets the element's value to an empty string. This direct modification is called a side effect. - getTodoText() - This is a quick helper function that returns the value inside of our textfield. Notice how some functions return values and how you can set that return to a variable.
- addTodo() - This is the primary logic of our todo app. Here's how the lines break down.
todois set to equal the first todo itemnewTodois a clone of todo. Passing true means it is a deep clone, so we get the todo's children as well. Cloning does not duplicate the DOM node. We'll need to insert it in step 4- We set the innerText of the
<span class='title'>to the value returned from getTodoTextNote that if we left off the
()we'd actully be assiging innerText to the 'function' instead of the function return - Insert our new todo into the todo's parent (the
ul), before our reference todo. insertBefore
- filter() - This function takes in a
filterNamestring, and abuttonwhich is a reference to the clicked button.- Remove any
selectedclass names - Add
selectedto the clicked button - Get all of the todos with querySelectAll, and then loop through them.
- Set the
hiddenproperty of each todo based on the filter/state combination
- Remove any
Triggering functions from click events
Now that we have a working addTodo function, we need a way to trigger it when the user is ready. This can be done in two ways.
- We can find the element with querySelector, then set its
onclickto our function
document.querySelector('.addTodo .submit').onclick = addTodo;
- We can add the function directly to our button in our HTML
<button onclick="addTodo()" class="submit">Add</button>
Today we'll use #2, as this is the way it will work in React as well.
Exercise
Write updateRemaining function
- Get a reference to the span with the
remainingclass, and store it in a variable - Use querySelectAll to get all of the todos.
- Set the
innerTextof the remaining span to the length of those todos. - Add updateRemaining() to addTodo
Write a clearCompleted function
- Get a reference to all of the todos with querySelectAll
- Use a
for (let todo of todos)loop to iterate over each todo - Inside the for loop write an
ifstatement to test if theinputinside of the todo has a checked value of trueHint: you can use querySelector on any HTML node to find child elements within
- Call
todo.remove()for any todo whos input check is true - After the loop is done, run
updateRemaining() - Attach this function to the footer button
- Test it out!