Start learning with our library of video tutorials taught by experts. Get started

HTML5 Projects: Advanced To-Do List
Illustration by

Saving and retrieving your list


From:

HTML5 Projects: Advanced To-Do List

with Joseph Lowery

Video: Saving and retrieving your list

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.

Watch this entire course now—plus get access to every course in the library. Each course includes high-quality videos taught by expert instructors.

Become a member
please wait ...
HTML5 Projects: Advanced To-Do List
45m 50s Intermediate Aug 26, 2013

Viewers: in countries Watching now:

The HTML5 Projects series combines HTML5, CSS3, and recent JavaScript API technologies to enhance your web projects with interactivity and multimedia. This installment shows how to create a basic to-do list widget with an editable field so users can enter their tasks and delete them as they are completed, and a more advanced version with all the bells and whistles, including a drag-and-drop interface that allows for quick and easy sorting. Author Joseph Lowery covers HTML5 technologies such as web databases, content editing in the browser, and CSS transitions and transforms.

Topics include:
  • Making content editable
  • Saving edits
  • Setting up the core files for your app
  • Removing list items
  • Enabling drag-and-drop sorting
Subjects:
Design Web User Experience Web Design Projects
Software:
HTML JavaScript CSS
Author:
Joseph Lowery

Saving and retrieving your list

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.

There are currently no FAQs about HTML5 Projects: Advanced To-Do List.

 
Share a link to this course

What are exercise files?

Exercise files are the same files the author uses in the course. Save time by downloading the author's files instead of setting up your own files, and learn by following along with the instructor.

Can I take this course without the exercise files?

Yes! If you decide you would like the exercise files later, you can upgrade to a premium account any time.

Become a member Download sample files See plans and pricing

Please wait... please wait ...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.
Upgrade now


Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ.

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

join now Upgrade now

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.


Mark all as unwatched Cancel

Congratulations

You have completed HTML5 Projects: Advanced To-Do List.

Return to your organization's learning portal to continue training, or close this page.


OK
Become a member to add this course to a playlist

Join today and get unlimited access to the entire library of video courses—and create as many playlists as you like.

Get started

Already a member?

Become a member to like this course.

Join today and get unlimited access to the entire library of video courses.

Get started

Already a member?

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

The classic layout automatically defaults to the latest Flash Player.

To choose a different player, hold the cursor over your name at the top right of any lynda.com page and choose Site preferencesfrom the dropdown menu.

Continue to classic layout Stay on new layout
Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

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.

Are you sure you want to delete this note?

No

Notes cannot be added for locked videos.

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.