First time with Js help

(code in comments)

Im trying to make a todo-list in javascript with local storage but it doest work, any idea what the problem is?

Posted Dec 12, 2016, 1:40 AM UTC

13
Hyrax_OP•

var $todolist = $('#todolist'), $saveButton = $('#save'), $clearButton = $('#clear');

function init() {
    
    $saveButton.on('click', saveList);
    $clearButton.on('click', clearList);
    loadList();
    
}

function saveList(){
    
    var list= $todolist.html ();
    localStorage.setItem('todolist', list);
    
}

function clearList(){
    
    localStorage.clear();
    location.reload();
}

function loadList(){
    var savedList = localStorage.getItem('todolist')
    
    if(savedList){
        $todolist.html(savedList);
    }
}
1
jcunews1•

The code looks good to me. The error is probably at the other code. e.g. init() never called; jQuery is not loaded; HTML element with relevant ID is missing, the script is executed before the relevant HTML elements are parsed by the browser, etc.

1
nwilliams36•

Do a console log of list in the saveList function to see what are trying to save. If I remember right with localStorage both the key and the value must be strings.

If you are looking at this on Chrome, use the developer tools to look at what is being stored in localStorage, its under the Application tab. Also the console will log any errors so I would also look there.

1