JS:
var todoList = [];
var out;
var mass = document.body;
if (localStorage.getItem('todo') != undefined) {
todoList = JSON.parse(localStorage.getItem('todo'));
out1();
}
document.getElementById('add').onclick = function () {
var val = document.getElementById('in').value;
var temp = {};
temp.todo = val;
temp.check = false;
var i = todoList.length;
if (val != "") {
todoList[i] = temp;
}
console.log(todoList);
out1();
localStorage.setItem('todo', JSON.stringify(todoList));
//a += "<div id="out"></div>"
}
function out1() {
out = "";
for (var key in todoList) {
if (todoList[key].check == true) {
out += '<input type="checkbox" id="test" name="fun" checked>';
}
else {
out += '<input type="checkbox" id="test" name="fun">';
}
out += "<label name='idk'>" + todoList[key].todo + "</label>" + "<br/>";
}
document.getElementById('out').innerHTML = out;
}
document.getElementById('delete').onclick = function () {
var del = document.getElementById('out');
var doc = document.getElementById('test');
//del.removeChild(doc);
document.getElementById('out').innerHTML = "";
out = "";
todoList = [];
localStorage.clear();
}
document.getElementById('delOne').onclick = function () {
var br = document.getElementsByTagName('br');
var chbx = document.getElementsByName('fun');
var idl = document.getElementsByName('idk');
for (var j = 0; j < chbx.length; j++) {
if (chbx[j].checked) {
chbx[j].remove();
idl[j].remove();
br[j].remove();
delete todoList[j];
}
}
console.log(todoList);
}
HTML:
<body>
<input type="text" id="in">
<button id="add">Добавить</button>
<button id="delete">Очистить</button>
<button id="delOne">Удалить</button>
<hr/>
<div id="out"></div>
</body>
</html>
Вопрос: Как сделать так, чтобы выбранный элемент удалялся из localSorage и элемент самого массива?