1
<body>
    <input type="text" id="in">
    <button id="add">Добавить</button>
    <button id="add1">saved</button>
    <hr>
    <div id="out"></div>
</body>
let todoList = [];

if (localStorage.getItem('todo') != undefined) {
    todoList = JSON.parse(localStorage.getItem('todo'));
    out();
}

document.getElementById('add').onclick = function () {
    let d = document.getElementById('in').value;
    let temp = {};
    let i = todoList.length;
    temp.todo = d;
    temp.check = false;
    todoList[i] = temp;
    out();
    localStorage.setItem('todo', JSON.stringify(todoList));
}

function out() {
    let out = "";
    for (let key in todoList) {
        document.getElementById('add1').onclick = function () {
            let q = document.querySelectorAll('.p') 
            for (let key1 of q) {
                if (key1.checked) {
                    todoList[key].check = true;
                    console.log('check');
                }
            }
        }
        if (todoList[key].check == true) {
            out += '<input type="checkbox" class="p" checked>';
        }
        else {
            out += '<input type="checkbox" class="p">'
        }

    out += todoList[key].todo + '<br>' ;
    }
    document.getElementById('out').innerHTML = out;
}

1 ответ 1

1

https://developer.mozilla.org/en-US/docs/Web/API/Storage/getItem

if (localStorage.getItem('todo') != null) {

или просто

if (localStorage.getItem('todo')) {

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.