I saw this tutorial the other day, it’s cool but I fancied taking it a step further and make it useful as an app rather than just a fancy way to show some structured data on the screen. Here’s a list of the features which I wanted to add:
- being able to edit the title and content of the notes
- being able to save all your notes
- being able to add new notes
- being able to delete notes you no longer want
- randomize the colour of the notes more
HTML and CSS changes
I initially considered using the new contenteditable attribute in HTML5 to make the title and content editable, but settled on using textarea instead as it offers better support for multiple line text and you can easily add placeholder text as well as set a limit on the length of the title.
I added two images as buttons to add a new note and save the state of all the notes and put a a border around the notes to make them stand out a little more.
Here’s the updated HTML:
And the CSS to go along with it:
There’s a couple of things to note here:
- the textarea element has a transparent background
- the textarea element is not resizable
- the textarea element has a placeholder text
Here’s what the page looks like now:
Adding a new note
In the CSS above, you can see that I had defined three CSS class:
when we create a new note, all we need to do to randomize its colour is to add one of these classes to the element randomly.
Here’s the event handler for the ‘new’ image button:
as you can see, this event handler calls the addNewNote function which is responsible for adding a new note to the notes unordered list and sets its class, title or content depending on the arguments. In this particular usage it’ll pick one of the three colour classes randomly and add the class to the new note.
Once a note has been added, I then get a reference to it and create an event handler for its close button so that when its clicked it’ll remove this note from the DOM.
The addNoteEvent function is then charged with adding the focus and hover in/out event handlers to the note to show and hide the close button so that it’s only shown when the note is focused/hovered on.
Saving the notes
I have previously cover the use of HTML5’s local and session storage in this post already, what I’m doing here is very similar but a little more involved.
When the ‘save’ button is clicked, I need to inspect each of the notes and remember the class, title text (remember, you can’t get the text that’s been entered into the textarea by getting the HTML of the element) and content text in a bespoke object, the resulting array of objects is then JSON serialized using Douglas Crockford‘s Json.js library. It this is JSON string that’s saved into the local storage.
On the other end, when the page has been loaded and the DOM event fired (i.e. when JQuery.ready fires) I will need to check if the JSON string is in the local storage, if it is, then deserialize the string back to an object array and add the notes back in in the same order:
You can try out the full demo at:
or using the short link:
Thanks to Joey Beechey for pointing out to me in the comment that the demo doesn’t work in Safari and after a bit of digging I found that the problem is with the addNewNote function which has a parameter named ‘class‘ which is a reserved word. I found another reference of this problem here too, which mentions the same problem but with the variable name ‘abstract‘ instead!
I’ve also put the whole thing into a zip file which you can download from here, so feel free to play around with it yourself!