Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

138
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda