Показать сообщение отдельно
  #3 (permalink)  
Старый 09.10.2019, 17:15
Аватар для рони
Профессор
Отправить личное сообщение для рони Посмотреть профиль Найти все сообщения от рони
 
Регистрация: 27.05.2010
Сообщений: 33,068


если id не нужен
<!DOCTYPE html>

<html>
<head>
    <title>Untitled</title>
    <meta charset="utf-8">

</head>

<body>
<ul class="todos"></ul>
<input type="text" class="todotext">
<button class="addtodo">add todo</button>
<script>
    let todos = [];
    const todosEl = document.querySelector('.todos');
    const todoTextEl = document.querySelector('.todotext');
    const update = () => {
        todosEl.innerHTML = todos.map((todo,i)=> `<li>${i + 1} - ${todo.text} <button data-todo-id=${i}>x</button></li>`).join('')
    }
    document.querySelector('.addtodo').addEventListener('click', () => {
        todos.push({
           text: todoTextEl.value
        })
        todoTextEl.value = ''
        update()
    })

    todosEl.addEventListener('click', ({target}) => {
        target = target.closest('[data-todo-id]');
        if(target){
           todos.splice(target.dataset.todoId, 1);
           update()
        }
    })
</script>

</body>
</html>
Ответить с цитированием