if (li.onclick) {
todo.done = true;
li.classList.add('list__item_done');
}
<!DOCTYPE html>
<html>
<head>
<link href='http://fonts.googleapis.com/css?family=Open+Sans' rel='stylesheet' type='text/css'>
<link rel = 'stylesheet' href = 'style.css'>
</head>
<body>
<script src = 'todo.js'></script>
<h1 class = "title">ToDo List</h1>
<div class = "todo-list">
<div class = "actions">
<input class = "task-input" type = "text"/>
<button class = "btn" onclick = "newElement()">Create</button>
</div>
<ul class = "list">
</ul>
</div>
<script>
todosRender();
</script>
</body>
</html>
const arr = [];
var ul = document.querySelector('ul');
function newElement() {
var inputValue = document.querySelector('input').value;
if (inputValue.length !== 0) {
arr.push({text: inputValue, done: false});
localStorage.setItem('taskslist', JSON.stringify(arr));
}
var li = document.createElement('li');
li.classList.add("list__item");
li.appendChild(
document.createTextNode(inputValue)
);
document.querySelector('ul').appendChild(li);
document.querySelector('input').value = "";
};
function todosRender () {
var raw = localStorage.getItem('taskslist');
var todos = JSON.parse(raw);
if (todos) {
for (todo of todos) {
var li = document.createElement('li');
li.classList.add("list__item");
li.appendChild(
document.createTextNode(todo.text)
);
document.querySelector('ul').appendChild(li);
if (li.onclick) {
todo.done = true;
li.classList.add('list__item_done');
}
}
}
};
window.addEventListener('storage', ({ url, key, newValue: value }) => {
console.log(`Key '${key}' changed value to '${value}' from '${url}'`);
arr.push(value);
todosRender();
});
if (inputValue.length !== 0) {
arr.push({text: inputValue, done: false});
localStorage.setItem('taskslist', JSON.stringify(arr));
}
if (!inputValue.length?.trim()) {
arr.push({text: inputValue, done: false});
localStorage.setItem('taskslist', JSON.stringify(arr));
}
if (!inputValue.length?.trim()) {
arr.push({
text: inputValue,
done: false,
id: Math.random().toString(),
});
localStorage.setItem('taskslist', JSON.stringify(arr));
}