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

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

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 Report

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 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!