How to remove the nth item in a list using javascsript

I know how to remove all items from a list. I do not know how to remove only one item. Let's say I want to remove the 3rd list of items

disorder list. let's say I have the following:
<ul id="parent"> <li> <label>this is the fist item</label> </li> <li> <label>this is the second item</label> </li> <li> <label>this is the third item</label> </li> <li> <label>this is the fourth item</label> </li> </ul> 

his eassy to remove a fist child or last child. The list that I create is built dynamically, and it will be nice if I can remove the nth child. It would be nice if I could do something like document.getElementById ("someElemet"). Delete

+4
source share
2 answers
 var ul = document.getElementById('parent'); var liToKill = ul.childNodes[2]; liToKill.parentNode.removeChild( liToKill ); // or ul.removeChild( ... ) 
+11
source

Sample code to remove the third element (index number 2):

 document.getElementById("parent").removeChild(document.getElementById("parent").children[2]); 
+3
source

Source: https://habr.com/ru/post/1345623/


All Articles