While researching material for this book, I came across a great post by Koes Bong at http://web.koesbong.com/2011/01/24/sortable-and-editable-to-do-list-using-html5s-localstorage/ on creating a sortable to-do list using Local Storage. It shows off a number of aspects of using Local Storage that we have covered in this book perfectly. Let's take a look at how.
For this task, you'll need a few things in addition to your normal text editor and the code that accompanies this book. I've assumed that local copies of all of the following plugins have been saved for the purposes of this task:
25px
square, in PNG format. If yours is different, you will need to edit the code accordingly. For this recipe, I've assumed you have something in PNG format.jquery-ui-1.9.2.customcsssmoothnessimages
for the image.Perform the following steps, for building a simple to-do list:
To-Do List
somewhere on your PC, and save copies of the following folders from the code available with this book: css
, img
, and js
.todolist.html
within the To-Do List
folder:<!DOCTYPE html> <html> <head> <title>Test To-do List using Local Storage</title> <meta charset="utf-8" /> <link rel="stylesheet" href="css/base.css" type="text/css" /> </head> <body> <div id="container"> <h1>Test To-Do List</h1> <form id="todo-form"> <input id="todo" type="text" /> <input id="submit" class="button medium red" type="submit" value="Add to List"> </form> <ul id="show-items"></ul> <a href="#" id="clear-all">Clear All</a> </div> <script src="js/jquery-1.8.3.min.js"></script> <script src="js/jquery-ui.js"></script> <script src="js/jquery.inlineedit.js"></script> <script src="js/pubsub.js"></script> <script src="js/base.js"></script> </body> </html>
Although there may appear to be a fair chunk of code in this recipe, most of the magic is in the base.js
file. This includes all of the method handlers to manage the storing and retrieving of content from Local Storage.
If we look at the script closely, we see that it starts with a request to retrieve content from LocalStorage
, which is then fed into a for
loop to populate the list shown on screen. The methods that follow this are then responsible for adding, removing, or sorting content accordingly. The code then contains some additional functionality to allow in-line editing of each task using the jquery.inlineedit.js
plugin, as well as functionality to fade in and out the X logo on each task item. Finally the code finishes with a method to manage the saving of content to LocalStorage
, using the jQuery pub/sub plugin from Peter Higgins.
This is very much a proof of concept. You may well find it doesn't work in Internet Explorer, for example! It is worth studying the code though—there are some great examples of how you can use jQuery to manage Local Storage. I would suggest though that you will want to work on it if you use it in your own projects—it needs altering to make compatible with IE; it could also be turned into a plugin, which will certainly make it more useful.
So far, we've looked at how you can use Local Storage in a web application of some description. What about using it in a content management system or blog? This is equally possible; over the next two recipes, we'll take a look at how you can achieve both, beginning with implementing it in a simple "Contact Us" form.