Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
In the basic version of the To Do list, we had a link for saving the list whenever you wanted, but that's a little, well, basic, don't you think? It'd be much better if we could automagically store and retrieve the list. And that's what we'll do in this final lesson of the Advanced To Do list, courtesy of the HTML5 functionality, Local Storage. And now we see, index.HTM file from the chapter 3 > 03_05 folder > Open. And again, let's head down south in the file. All the way down towards the bottom, and I'm going to stop around line 158, so we can see the bulk of our add to do on click function.
Because we need to store our list under a number of different scenarios, we're going to be adding the same code to five different existing code chunks. We're going to put our first one right at the end of this routine. We're adding a new list item, so I'll scroll down just a little bit there, and right after the sortable group, we'll create a new line, and the code that we're going to use throughout, is just a single line. And it's localStorage, it's an upper case s, .setItem(), and two arguments.
The first is the name of the localStorage item we want to set, and I've called this todoListPlus. That's just to differentiate it from the to do list, which was the basic one. Comma, and then what are we putting in there, we're putting in the list, you'll recall that was one of the variables that we set up up front, .innerHTML. And that's all we need. So now, what I'm going to do, is just copy this line, and start to put it in the other location set we needed. Another time we want to automatically save, is after we make any edits to the list items.
So, let me scroll down a tad, and there on line 198, is the list on change, and I'm going to add in my missing little semi-colon here. And then pace in my local storage line. Naturally, we also want to remember any updates to the order of the list, so let's fill out that sortable function next, which is right after that. I'll tab in a little bit here, paste in my code. Looks like I've forgotten to put the semi-colon after that, so let me to go back and correct that mistake, so it doesn't come back to haunt us, and I'll scroll back down.
Let me recapture that with the semicolon, so I don't repeat that error. Now we certainly don't want any removed items left hanging around, so let's add the local storage code to the remove list item function, starts on line 218. Finally, we certainly want to remember that we've cleared out the list if we do, so let me scroll all the way down, so let's add the code to our do clear all on click function. That takes care of the storing of the list items, but what about the retrieving? Well, that needs to happen whenever the page loads.
So we're going to write one function, which will be called whenever that takes place, and we'll handle that by putting the function call in the root of document ready. So right after the do clear all, going to create a couple of lines, and we'll call this function loadToDo, and I'll scroll down a bit, so I can start in a good place. Now let's create the function, put in the keyword function, my function name loadToDo, open and close parentheses, no arguments, and open and close curly brace. And the first thing we want to do is to check if our local storage item exists.
No reason to do anything if it doesn't. So if local storage dot get item, open and close parentheses, single quotes, and we put in the name of our local storage item, which is to do list plus. And then following the second closing parenthesis, we put in a curly brace pair, so if our local storage item exists, we want to get it and then set it to the inner HTML of the list. So the list dot inner HTML, set that equal to local storage dot get item, and the name of the item again, to do list plus.
The rest is really just housekeeping, but kind of essential housekeeping. We first need to make sure that it's sortable, by bringing in our sortable routines. So I'll scroll up to line 180, and grab the lines of code through 183, copy that, scroll back down, and we'll paste it in, see what kind of a manipulation with tabs that I have to work with here. Looks like I need to do a little adjusting, to make it all line up properly. After the sorting has been re-enabled, we want to either show or not show the clear all link, according to the number of list items.
So we've already written that code. Let's go grab that. That starts on line 170. So I'll select and copy that, and paste it in. Let me select again and tab it over. And I'll get rid of this extra line here so everything neatens up. OK. All done. Let's save our page, and fire this puppy up. So I'm going to go back to Chrome, where I've got the page already set up. Now before I hit refresh, I want to show you how you can deal with local storage during your testing phase for your app. Now if you're working with Chrome, you can go to View > Developer > Developer Tools and then click on Resources.
Now I'll stress that this is for Chrome, it's also similar in Safari. Firefox has its own set of developer tools, but both allow you to see what local storage there is, if you use it at all. So if we had any, if you recall during the demo, I cleared out all of the items at the end of each of the two demos. One for basic and one for advanced. So we don't really have any local storage here now, but if we did, it would show up here. And I'll show you that in just one second. So let me close this now, and I'll hit refresh, and let's go ahead and try out our list items.
As you can see when I hit refresh, we went to the field that's, our little auto focus working, so I'll put in get milk, and let's click on our add list item this time, that works. Alright let's add in another one let's see, wait for tonight. This times I'll press Tab and hit Enter. Now that worked, let me scroll down, and there's our clear all, and as you can see our x's are showing up. Let's add one more list item, and this is a shout out to all of those Pinky and the Brain fanatics still out there.
Where they always waited for tonight to try and take over the world. You gotta keep Pinky and the Brain alive, you know. So let's test some functionality. Let's try our editing. So instead of get milk, let's change that to missiles, and maybe we'll get the missiles later in the evening. And then try to take over the world, okay, well maybe we won't even wait for tonight, let's get rid of that. Okay, so now I'm ready to refresh the page, to see if our local storage is working correctly, a quick refresh. And it looks good. Now before I do the final test of clearing out the list, I want to show you what the local storage storage looks like from the developer prospective.
So let's go to View > Developer > Developer Tools, and we're already on the Resources tab, so I'll expand Local Storage this time, and there's my Local Host, Local Storage. There's the to do list plus entry. And as you can see, it's a series of list items, perfect. Let's try clear all, and we're back to square one. Excellent, job well done. Your advanced to do list is now fully operational.
Get unlimited access to all courses for just $25/month.Become a member
117 Video lessons · 42162 Viewers
119 Video lessons · 53656 Viewers
65 Video lessons · 14072 Viewers
113 Video lessons · 82712 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.
Your file was successfully uploaded.