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

142
Visualizações
The node element is deleted only the second time by function JS

The node element is deleted only the second time, and those created using the add function are deleted the first time

function move(o) {
  var parent = o.parentNode;
  parent.removeChild(o);
  parent.insertBefore(o, parent.firstChild);
}

function add() {
  var node = document.createElement('li');
  var textnode = document.createTextNode("new");
  node.appendChild(textnode);
  node.setAttribute("onclick", "move(this)");
  document.getElementById("table").appendChild(node);
  id = "myList"
}

function del() {
  o = document.getElementById("table");
  o.removeChild(o.firstChild);
}
<ol id='table'>
  <li onclick="move(this)">1</li>
  <li onclick="move(this)">2</li>
  <li onclick="move(this)">3</li>
  <li onclick="move(this)">4</li>
  <li onclick="move(this)">5</li>
  <li onclick="move(this)">6</li>
</ol>
<button onclick="add()">Append</button> <button onclick="del()">Del</button>

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You have to use

o.removeChild(o.firstElementChild);

in your del() function.

So this is the updated code:

<ol id="table">
  <li onclick="move(this)">1</li>
  <li onclick="move(this)">2</li>
  <li onclick="move(this)">3</li>
  <li onclick="move(this)">4</li>
  <li onclick="move(this)">5</li>
  <li onclick="move(this)">6</li>
</ol>
<button onclick="add()">Append</button> <button onclick="del()">Del</button>
<script>
  function move(o) {
    var parent = o.parentNode;
    parent.removeChild(o);
    parent.insertBefore(o, parent.firstChild);
  }
  function add() {
    var node = document.createElement("li");
    var textnode = document.createTextNode("new");
    node.appendChild(textnode);
    node.setAttribute("onclick", "move(this)");
    document.getElementById("table").appendChild(node);
    id = "myList";
  }
  function del() {
    o = document.getElementById("table");
    o.removeChild(o.firstElementChild);
  }
</script>
about 4 years ago · Juan Pablo Isaza Relatório

0

The firstChild is not an LI but a textnode

Use

 o.removeChild(o.firstElementChild);

Also delegate the click

const table = document.getElementById("table")
table.addEventListener("click",function(e) {
  const tgt = e.target;
  if (tgt.tagName==="LI") {
    move(tgt);
  }
})
function move(o) {
  var parent = o.parentNode;
  parent.removeChild(o);
  parent.insertBefore(o, parent.firstElementChild);
}

function add() {
  var node = document.createElement('li');
  var textnode = document.createTextNode("new");
  node.appendChild(textnode);
  table.appendChild(node);
}

function del() { 
  table.removeChild(table.firstElementChild);
}
<ol id='table'>
  <li>1</li>
  <li>2</li>
  <li>3</li>
  <li>4</li>
  <li>5</li>
  <li>6</li>
</ol>
<button onclick="add()">Append</button> <button onclick="del()">Del</button>

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