Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

203
Visualizações
Remove an element when button clicked

I've been trying to make simple to-do list to learn javascript and I have been able to add and display a task but I can't find a way to delete it. I have created a remove button but I can't make work, nothing happens when clicked. So any help would be great, thanks !

function handleSubmit(event){

    event.preventDefault();

    let value = getInputValue();

    if(value !== false){


        addToDom(value);

     
        eraseInput();
    }
}

const form = document.querySelector("#todo-list-form");
form.addEventListener('submit', handleSubmit);

function getInputValue() {
    const input = document.querySelector("#todo-input");

    let value = input.value;

    value = value.trim();

    if (value === '') {
        alert("You can not submit an empty field");
        return false;
    }
    else {
        return value;
    }
}

function addToDom(value) {
    const list = document.querySelector("#list");
    const removeButton = document.createElement("button");
    removeButton.innerHTML = 'remove';
    removeButton.classList.add("complete-btn");
    list.appendChild(removeButton);
    list.innerHTML += "<li>" + value + "</li>";
    console.log(list);
    removeButton.addEventListener('click', removeItem)
}

function removeItem(event){
    event.parentElement.remove();

}

function eraseInput() {
    const input = document.querySelector("#todo-input");

    input.value = "";

    input.focus();
}
almost 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

The issue is because you define and add the removeButton to the DOM, but immediately after that you overwrite the HTML of the parent element, which removes the button.

To fix the issue you need to be consistent in how you create and append the elements to the DOM.

In addition, as @Teemu pointed out in the comments, you need to use event.target.parentElement in your removeItem() function.

function addToDom(value) {
  const removeButton = document.createElement("button");
  removeButton.innerHTML = 'remove';
  removeButton.classList.add("complete-btn");
  removeButton.addEventListener('click', removeItem);
  
  const li = document.createElement('li');
  li.textContent = value;
  li.appendChild(removeButton);

  document.querySelector("#list").appendChild(li);
}

function removeItem(event){
  event.target.parentElement.remove();
}

Here's a full working example:

function handleSubmit(event) {
  event.preventDefault();

  let value = getInputValue();
  if (value !== false) {
    addToDom(value);
    eraseInput();
  }
}

const form = document.querySelector("#todo-list-form");
form.addEventListener('submit', handleSubmit);

function getInputValue() {
  const input = document.querySelector("#todo-input");

  let value = input.value;
  value = value.trim();

  if (value === '') {
    alert("You can not submit an empty field");
    return false;
  } else {
    return value;
  }
}

function addToDom(value) {
  const removeButton = document.createElement("button");
  removeButton.innerHTML = 'remove';
  removeButton.classList.add("complete-btn");
  removeButton.addEventListener('click', removeItem);

  const li = document.createElement('li');
  li.textContent = value;
  li.appendChild(removeButton);

  document.querySelector("#list").appendChild(li);
}

function removeItem(event) {
  event.target.parentElement.remove();
}

function eraseInput() {
  const input = document.querySelector("#todo-input");
  input.value = "";
  input.focus();
}
ul button { margin: 0 0 0 5px; }
<form id="todo-list-form">
  Item: <input type="text" id="todo-input" />
  <button type="submit">Add</button>
  <ul id="list"></ul>
</form>

almost 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda