0
How do I remove the item from the list that is generated after the event that creates it? I am trying to place an event that has the function of removing the item, but when I run the code it turns me an error and the remove button does not work.
'use strict';
//input do header
const input = document.querySelector("input")
//Botão do Header
const headerBtn = document.querySelector("#btnHeader")
//ul
const list = document.querySelector("#list")
//Class do botão de remover item
const listBtn = document.querySelector(".btnList")
//Função que cria um item no HTML
const model = (item) => {
const itemList = document.createElement("li")
itemList.innerHTML = `
${item}
<button class="btnList">Remover</button>
`
list.appendChild(itemList)
}
//Evento que cria o item ao clicar no botão
headerBtn.addEventListener("click", function (event) {
if(input.value === "" || input.value === " ") {
alert("Preencha o campo com um item")
} else {
model(input.value)
input.value = ""
}
event.preventDefault()
})
//Evento que remove o item ao clicar no botão
<!DOCTYPE html>
<html lang="pt-br">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="style.css">
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Roboto+Condensed:wght@400;700&display=swap" rel="stylesheet">
<title>To do list</title>
</head>
<body>
<header>
<h1>
To do List
</h1>
</header>
<main>
<section id="add">
<form>
<input type="text" placeholder="O que deseja listar?" autocomplete="off">
<button id="btnHeader">Adicionar</button>
</form>
</section>
<section>
<ul id="list">
</ul>
</section>
</main>
<script src="script.js"></script>
</body>
</html>
Put on button one
onclick="this.parentNode.outerHTML = ''"
. This will remove the<li>
where is the button.– Sam
On the remove button? But I also have the problem in the removal event, because the li is generated after finalizing the create event, where I can call this event?
– PdroLucas