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

291
Vistas
Can't use removeChild() to delete the element just appended to list. Can only delete the ones at the beginning

This is two button. One is "appendChild" ,One is "Remove Child".

List ----Before------------

  1. one

  2. two

  3. three

  4. appended

  5. appended

-------------------------

After appended two new elements, I want to remove the last one. But when I press the remove button.

List ---After-------------

  1. one

  2. two

  3. (?????????? be deleted)

  4. appended

  5. appended

-------------------------

The appended elements are still there. I see many solve this problem with jQuery, but I am totally new. I am wondering if JavaScript has the solution.

apd.onclick = function() {
    let apd = document.getElementById('apd')
    let node = document.createElement('li')
    node.className = 'added'
    let textnode = document.createTextNode('this is node content')
    node.appendChild(textnode)
  
    document.getElementById('list').appendChild(node)
}

let list = document.getElementById('list')
let rem = document.getElementById('rem')
rem.onclick = function() {
    let node = document.createElement('li')
  
    document.getElementById('list').removeChild(list.children[0])
}
<ul id="list">
  <li>1</li>
  <li>2</li>
  <li>3</li>
  <li>4</li>
  <li>5</li>
</ul>
<button id="apd">click me to append child!</button>
<button id="rem">click me to remove child!</button>

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

You can use list.lastElementChild.remove(); for delete problem and instead of onclick event use addEventListener as suggest by @T.J- Crowder into comment like:

let list = document.getElementById('list')
let rem = document.getElementById('rem')
let apd = document.getElementById('apd')
apd.addEventListener('click', () => {
  let node = document.createElement('li')
  let textnode = document.createTextNode('this is node content')
  node.className = 'added'  
  node.appendChild(textnode)
  list.appendChild(node)
});
rem.addEventListener('click', () => {
  list.lastElementChild.remove();
  //or if you need application for IE use list.removeChild(list.lastElementChild);
});
<ul id="list">
  <li>123125151</li>
  <li>123125151</li>
  <li>123125151</li>
  <li>123125151</li>
  <li>123125151</li>
</ul>
<button id="apd">click me to append child!</button>
<button id="rem">click me to remove child!</button>

Reference:

  • Element.lastElementChild
  • Element.remove() - note: not on Internet Explorer, you'd need removeChild on the parent instead
  • EventTarget.addEventListener()
about 4 years ago · Juan Pablo Isaza Denunciar

0

You are deleting the first one and not the last one.

Instead of list.children[0], you can use list.children[list.children.length - 1] which will be the last one.

apd.onclick = function() {
    let apd = document.getElementById('apd')
    let node = document.createElement('li')
    node.className = 'added'
    let textnode = document.createTextNode('this is node content')
    node.appendChild(textnode)
  
    document.getElementById('list').appendChild(node)
}

let list = document.getElementById('list')
let rem = document.getElementById('rem')
rem.onclick = function() {
    let node = document.createElement('li')
  
    document.getElementById('list').removeChild(list.children[list.children.length - 1])
}
<ul id="list">
  <li>1</li>
  <li>2</li>
  <li>3</li>
  <li>4</li>
  <li>5</li>
</ul>
<button id="apd">click me to append child!</button>
<button id="rem">click me to remove child!</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