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

177
Visualizações
How do I get the splice() method to work in my todo list?

I´m building a simple todo list and will use the splice method to remove items from the list. I can't get it to work. I get the spliced item to the console so the "connection" between my remove button and my remove function seems to work. I also tried the slice method and that works, but for some reason the splice method won't work. Below is the array that I use to push in my new items to my class, the function that creates the HTML for the new todo items and the remove items function. Any idea why the splice method won't work?

//ARRAY FOR NEW TODO ITEMS
let newTodo: Todo[] = [];

// FUNCTION THAT CREATES THE HTML FOR THE NEW TODO ITEM

    let doneBtn = document.createElement("button");

function htmltask() {
  let taskDiv = document.createElement("div");

  taskDiv.innerHTML = "";

  for (let i = 0; i < newTodo.length; i++) {
    taskDiv.className = "taskDiv";
    taskDiv.innerHTML = newTodo[i].todoItem;

    let btnContainer = document.createElement("div");
    btnContainer.className = "btnContainer";

    // REMOVEBTN //
    let removeBtn = document.createElement("button");
    removeBtn.innerHTML = "REMOVE";
    removeBtn.addEventListener("click", (e) => {
      remove(e, i);
    });

    // DONEBTN //
    let doneBtn = document.createElement("button");
    doneBtn.innerHTML = "DONE";
    doneBtn.className = "doneBtn";

    newTaskDiv.appendChild(taskDiv);
    taskDiv.appendChild(btnContainer);
    btnContainer.appendChild(doneBtn);
    btnContainer.appendChild(removeBtn);
  }

  localStorage.setItem("newTodo", JSON.stringify(newTodo));
}

// FUNCTION TO REMOVE ITEMS
function remove(e: Event, i: number) {
  newTodo.splice(i, 1);
  htmltask();
}
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Changed your code a bit, now it works:

<div>
  <button onClick="htmltask()">Action!</button>
  <div id="tasks" />
</div>
type Todo = { todoItem: string }

let newTodo: Todo[] = [
  { todoItem: 'Eat'},
  { todoItem: 'Sleep'},
  { todoItem: 'Code'},
];

function htmltask() {
  const allTasksDiv = document.getElementById("tasks");
  allTasksDiv.innerHTML = "";

  for (let i = 0; i < newTodo.length; i++) {
    const taskDiv = document.createElement("div");
    taskDiv.className = "taskDiv";
    taskDiv.innerText = newTodo[i].todoItem;

    const btnContainer = document.createElement("div");
    btnContainer.className = "btnContainer";

    // REMOVEBTN //
    const removeBtn = document.createElement("button");
    removeBtn.innerText = "REMOVE";
    removeBtn.addEventListener("click", (e) => remove(e, i));

    // DONEBTN //
    let doneBtn = document.createElement("button");
    doneBtn.innerText = "DONE";
    doneBtn.className = "doneBtn";

    btnContainer.appendChild(doneBtn);
    btnContainer.appendChild(removeBtn);
    taskDiv.appendChild(btnContainer);
    allTasksDiv.appendChild(taskDiv);
  }

  localStorage.setItem("newTodo", JSON.stringify(newTodo));
}

function remove(e: Event, i: number) {
  newTodo.splice(i, 1);
  htmltask();
}

Though I would recommend remove item in another way: search for the item's div and remove it. Instead of recreating the whole todos block from scratch.

about 4 years ago · Juan Pablo Isaza 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