1

i am trying to build post it notes. I am reading Head First Series. I did this code. but somehow it's no working.

<form action="post">
    <input id="note_text" type="text" placeholder="enter your Note">
    <input type="button" id="add_button" value="Add Note">

</form>
<ul id="postItNotesList">

    <li>This is my very first note.</li>

    <li>This is my very Second note.</li>

</ul>

And here is the Js

window.onload=init;

// Add Sticky to Page

function addStickyToPage(value) {
    var sticky = document.createElement("li");
    span.setAttribute("class", "sticky");
    document.getElementById("postItNotesList").appendChild(sticky);
}



// Create and get Sticky Note into the localStorage
function createSticky() {
    var value = document.getElementById("note_text").value;
    var key = "sticky_" + localStorage.length;
    localStorage.setItem(key, value);
    addStickyToPage(value);
}



function init() {

    var button = document.getElementById("add_button");
    button.onclick = createSticky;



    for (var i = 0; i < localStorage.length; i++) {
        var key = localStorage.key(i);
        if (key.substring(0, 6) == "sticky") {
            var value = localStorage.getItem(key);
            addStickyToPage(value);
        }
    }


}

So i've buld up a fiddle so that you can easily check it out Here is the Fiddle Please tell me where i am doing it wrong. Thanks.

Cloudboy22
  • 1,496
  • 5
  • 21
  • 39

1 Answers1

1

I have updated your Fiddle. Note : first you should create DOM element and then append text to this element and finally append this node to you body so your code should be like this:

window.onload=init();

    function addStickyToPage(value) {
        var sticky = document.createElement("li");
        sticky.setAttribute("class", "sticky");
        var t = document.createTextNode(value);
        console.log(t);
        sticky.appendChild(t);
        document.getElementById("postItNotesList").appendChild(sticky);
    }

And also windows.onload = init()with brackets

Thanks

The Reason
  • 7,705
  • 4
  • 24
  • 42