DOM रिमूव चाइल्ड () मेथड HTML डॉक्यूमेंट में निर्दिष्ट नोड के निर्दिष्ट चाइल्ड नोड को लौटाता है और हटाता है।
सिंटैक्स
निम्नलिखित वाक्य रचना है -
node.removeChild(node);
उदाहरण
आइए हम removeChild() विधि का एक उदाहरण देखें -
<!DOCTYPE html>
<html>
<head>
<style>
html{
height:100%;
}
body{
text-align:center;
color:#fff;
background: linear-gradient(62deg, #FBAB7E 0%, #F7CE68 100%) center/cover no-repeat;
height:100%;
}
p{
font-weight:700;
font-size:1.2rem;
}
ul{
list-style-type: none;
padding:0;
}
.btn{
background:#0197F6;
border:none;
height:2rem;
border-radius:2px;
width:50%;
margin:2rem auto;
display:block;
color:#fff;
outline:none;
cursor:pointer;
}
.show{
font-size:1.5rem;
font-weight:bold;
}
</style>
</head>
<body>
<h1>Student Subjects</h1>
<p>Hi, My favourite subjects are:</p>
<ul id="subjectList">
<li>Physics</li>
<li>Chemistry</li>
<li>Maths</li>
<li>English</li>
</ul>
<button onclick="removeSubject()" class="btn">Remove Subject</button>
<script>
function removeSubject() {
var subList = document.getElementById("subjectList");
if (subList.hasChildNodes()) {
subList.removeChild(subList.childNodes[0]);
}
}
</script>
</body>
</html> आउटपुट
यह निम्नलिखित आउटपुट देगा -

“विषय निकालें . पर क्लिक करें सूची से विषय को हटाने के लिए बटन -
