Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

202
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!