Making a to-do app is often the first application you learn in Java scripting, but the biggest problem with such apps is that when you try to reload the page, those to-do’s disappear. However we know the solution – it’s using local storage.
How Local Storage Works
Local storage is one of the fields where native client applications have held a benefit over web applications. For native applications, the operating system usually offers an abstraction layer for storing application-specific data as preferences. Such values can be stored in INI files, XML files, and others due to platform convention. If a native client application requires local storage beyond key or value pairs, you can embed your own database or invent your own file format. While Java scripting, you can use one of two options to store data in the specification:
- Local storage keeps data with no expiration date and it’s one a designer usually uses if he or she wants to leave his or her to-do’s on the page for a long time.
- Session storage keeps data for one session. If you close the tab, your data will disappear.
All web storages store named key pairs in local storage, and this data will be saved even if you close the browser and turn off the computer.
Work With HTML
If you are thinking about a to-do list, what you need is:
- Place to-do with the help of an input.
- An input button to add your to-do.
- A button for clearing to-dos.
- A placeholder unordered list to place to-do in list items.
- A placeholder div to present an alert if you try to enter an empty to-do.
If you are thinking about the structure of a simple to-do app while Java scripting, the first thing you should do is to check for when the user clicks on the add button. Also, you should check if the input has an empty value. Check for a click on the add button as well as run a simple test to check if users filled the input with something. If they don’t do that, the alert div can fade in and stay for 1000ms and then fade out. You may return false so that the browser can’t read the rest of your script and add the list item.
The next thing a Java developer needs to do is to add a list item with the value in the input. He or she should do this so that when the user includes a to-do, it’ll go to the beginning of the list and then keep the item of the list to local storage.
If you have a standard jQuery, and when it comes to local storage, you should save a key and a value. The key is a name a Java developer sets for himself or herself and you may simply call it “todos,” and then you need to specify what you actually want to save.
The next step a Java developer should do is to check whether he or she has something on local storage already saved in the machine, he or she needs to place that in the page. So, if you gave your key the name todos, you need to check for its existence.
You may use a simple statement to check it and then you may merely get what you have on local storage and add that like the HTML of the to-do’s unordered list. If you test your simple app and reload the page, you will see that it is working and all that you have left to do is to develop a function for situations when a user decides to clear all the to-dos. When that happens, you will clear all local storage, reload the page for your changes take effect, and return false to prevent the hash in front of the url. After that, your app should work.