Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to remove parent element of a list group item

I have a list and in each list group item there is a delete button. I would like the delete button to remove the list item. At the moment i can only delete the button but i would like the whole li element to be removed

function remove(ev) {
  var elem = document.getElementById(ev.target.id);
  elem.this.parentNode.id.remove();
  console.log(elem.this.parentNode.id);
}
<ul class="list-group">
  <li class="list-group-item">
    <p>Item 1</p>
    <button type="button" id="btn1" onclick="remove(event)" class="Button">Click</button>
  </li>
  <li class="list-group-item">
    <p>Item 1</p>
    <button type="button" id="btn1" onclick="remove(event)" class="Button">Click</button>
  </li>
</ul>

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

You can use parentNode.remove()

function remove(ev) {
  var elem = ev.target; // get the delete button element
  elem.parentNode.remove(); // remove the parent li element
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading