The following code shows an example of the removeChild() method in JavaScript.
Programmingempire
Demonstrating Example of the removeChild() Method in JavaScript
The following code shows an unordered list with some list items. When the user double clicks on a list item, the function f1() is called. The function calls the removeChild() method. As a result, that particular list item gets removed.
<html>
<head>
<title>removeChild Method Demo</title>
<script>
function f1(index)
{
var x1=document.getElementById("d1");
var x2=document.getElementById("t2");
let num=Number(index);
x2.removeChild(x2.childNodes[num]);
num="";
}
</script>
<style>
.c1{
border: 4px solid #33ff99;
border-radius: 10px;
background-color: #5566aa;
color: #aaffff;
font-size: 20px;
text-align: center;
margin:5px;
padding: 10px;
}
.a1{
margin: 50px;
}
.c2{
background-color: #ddeeff;
color: #ff5566;
font-size: 25px;
text-align: left;
margin:40px;
padding: 5px;
width: 800px;
}
</style>
</head>
<body>
<div id="d1" class="c2"><br/><br/>
Example of removeChild() Method<br/>
Double Click on a list item to remove a color from the following list:
<br/>
<br/>
<ul id="t2">
<li ondblclick="f1(this.value)" value="0">Pink</li>
<li ondblclick="f1(this.value)" value="1">Red</li>
<li ondblclick="f1(this.value)" value="2">Orange</li>
<li ondblclick="f1(this.value)" value="3">Green</li>
<li ondblclick="f1(this.value)" value="4">Blue</li>
<li ondblclick="f1(this.value)" value="5">Purple</li>
<li ondblclick="f1(this.value)" value="6">Yellow</li>
</ul>
</div>
</body>
</html>
Output
![Removing a Child Node using the removeChild() Method](https://www.programmingempire.com/wp-content/uploads/2022/02/image-53-1024x521.png)
Further Reading
Evolution of JavaScript from ES1 to ES2020
Introduction to HTML DOM Methods in JavaScript
- Angular
- ASP.NET
- C
- C#
- C++
- CSS
- Dot Net Framework
- HTML
- IoT
- Java
- JavaScript
- Kotlin
- PHP
- Power Bi
- Python
- Scratch 3.0
- TypeScript
- VB.NET
![programmingempire](https://www.programmingempire.com/wp-content/uploads/2021/10/pefavicon.png)