Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member

Saving edits

From: HTML5 Projects: Advanced To-Do List

Video: Saving edits

Our content is editable, and now it's time to bring in a little extra functionality to make the app a bit more user-friendly. We want our users to be able to save their edits, if they so desire, so that they can continue making changes at a later date. We also want to give our users the ability to remove all list items. For both these goals we turn to the HTML5 technology known as localStorage. Let's start by setting up our triggers. Two links which will place right below the list items as you see here.

Saving edits

Our content is editable, and now it's time to bring in a little extra functionality to make the app a bit more user-friendly. We want our users to be able to save their edits, if they so desire, so that they can continue making changes at a later date. We also want to give our users the ability to remove all list items. For both these goals we turn to the HTML5 technology known as localStorage. Let's start by setting up our triggers. Two links which will place right below the list items as you see here.

So we're going to place this as I said right after the closing ul tag here, and we'll put it in its own paragraph. Let's put in the first link. So because we're just going to be using the link as a trigger of sorts, we'll set the href=javascript.void(), put that semicolon there, and we want to be sure to give this link an ID, and this ID will be saveAll. And within the link, we'll put the keywords Save All.

Let me hit Return, so we can put our next link on another line. And again it's going to be and close the tag and put in the text Clear All. With our links established, we're ready to code their functions. So for that we'll scroll down to the bottom of the page and we're going to put this right after the script tag that calls jQuery.

And this is a custom script tag. We'll set the type="text/javascript" close that and make a couple of lines for our code. Now we'll put in the jQuery document-ready function starting with $(document).ready(function() {}); and we'll make a little space and put in the and then hit Return to move that all the way down.

Now before I forget, I'm going to go ahead and put a semicolon at the end of that function call. Next we want to declare a variable which I'm going to call theList, = document.getElementById; and the ElementById that we're getting is for the unordered list which has an ID of the list. Semicolon at the end, now we're ready to code the first of two click functions.

This one will target the saveAll link. So we'll put in our $('#saveAll').click(function(e){}); and then Return, and again we'll put in our semicolon.

But the first thing we want to do here is to type e.preventDefault(); and that will stop the browser from expecting the link to be an actual link, and not a trigger as we're using it here. Again a semicolon at the end. And now we'll bring in localStorage. If they click the saveAll link, we want to use localStorage to set item and that item will call todoList. And what are we going to put in todoList? Well we're going to put in theList, that's the unordered list, innerHTML.

So that will be all of the tags that are contained within the unordered list. Okay, semicolon there. Because this next function is pretty similar, I'm just going to copy what we just did and then paste that in. Now instead of being triggered by saveAll, this is going to be triggered by the link with an id of clearAll. We'll keep the click(function) and e.preventDefault, but now we don't want to set an item, in fact, we want to clear all the items. And a function for that is just clear.

Now there is one last bit of code that needs to go in here. Once you've cleared the items, in order for that change to be reflected on the page, we'll have to reload the page. And the way to do that is to type location. reload(); and those functions are done. We're not quite finished, now we have to bring in the localStorage items when the page loads which will do by putting on its own line, the function called loadToDo, which is a call to a custom function.

We're just about to create. So function loadToDo(){}. Now we only want to attempt to load in these entries, if they exist. If we do it and there are no entries there, it's very possible that an error could be thrown. So let's check by using an if statement; if (localStorage.getItem('todoList'){}). If that item exists then let's set theList. innerHTML = localStorage.getItem.

So I am just going to highlight that, copy it, paste it in, and close it out with a semicolon. As I scanned over my code before I save the page, I realize that I've left off a parenthesis here, so I'm going to go ahead and bring that in. It's always a good idea to check your code one last time for those missing elements, and that also indicates that I've got an error there, and that was that parenthesis I got moved down, but otherwise I think this looks pretty good. So let's save the page and head over to the browser. I'll click Refresh.

Let's scroll down a bit and let's enter in a couple of items here. I thought maybe I would enter in a few movies that I've been wanting to see again. Of course Jaws is at the top of that list, and then perhaps Watchmen, Big, I always like, and then the animated feature 9. Let's save all and then I'm going to copy this link. Open up a new browser window and paste it in.

And there are our list items, good to go. But let's say I've watched all my flicks and now I'm ready to move on. I'll clear the list, and we're back to the default entry. Perfect! Our basic to-do list is up and running.

Show transcript

This video is part of

Image for HTML5 Projects: Advanced To-Do List
HTML5 Projects: Advanced To-Do List

13 video lessons · 9808 viewers

Joseph Lowery
Author

 

Start learning today

Get unlimited access to all courses for just $25/month.

Become a member
Sometimes @lynda teaches me how to use a program and sometimes Lynda.com changes my life forever. @JosefShutter
@lynda lynda.com is an absolute life saver when it comes to learning todays software. Definitely recommend it! #higherlearning @Michael_Caraway
@lynda The best thing online! Your database of courses is great! To the mark and very helpful. Thanks! @ru22more
Got to create something yesterday I never thought I could do. #thanks @lynda @Ngventurella
I really do love @lynda as a learning platform. Never stop learning and developing, it’s probably our greatest gift as a species! @soundslikedavid
@lynda just subscribed to lynda.com all I can say its brilliant join now trust me @ButchSamurai
@lynda is an awesome resource. The membership is priceless if you take advantage of it. @diabetic_techie
One of the best decision I made this year. Buy a 1yr subscription to @lynda @cybercaptive
guys lynda.com (@lynda) is the best. So far I’ve learned Java, principles of OO programming, and now learning about MS project @lucasmitchell
Signed back up to @lynda dot com. I’ve missed it!! Proper geeking out right now! #timetolearn #geek @JayGodbold
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.


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.

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

Your file was successfully uploaded.

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.