-2

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>
Teemu
  • 22,918
  • 7
  • 53
  • 106
  • Don't use `document.getElementById('list')` inside the click functions if you've already saved it in the variable `list`. – evolutionxbox Mar 28 '22 at 07:26
  • 1
    @T.J.Crowder that's exactly what he wrote. – Simone Rossaini Mar 28 '22 at 07:31
  • Two issues with the code that jump out: 1. You're relying on automatic DOM globals for `apd` in the initial `apd.onclick = /*...*/;`, but then using `getElementById` to look it up later. It's best not to rely on automatic DOM globals at all because of potential conflicts, etc.; use `getElementById` to get the element (just once is fine). 2. You're assigning functions to `onclick`. While that works, in general its best to use [`addEventListener`](https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener) instead. – T.J. Crowder Mar 28 '22 at 07:32
  • By `list.children[0]` you are explicitly asking for the first child element. – connexo Mar 28 '22 at 07:56

2 Answers2

1

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:

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
Simone Rossaini
  • 8,115
  • 1
  • 13
  • 34
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>
Wimanicesir
  • 4,606
  • 2
  • 11
  • 30