Last updated on February 14th, 2022 at 12:54 pm

Javascript is very helpful when you want to remove child div inside a parent div. This script can be used to remove any div defined inside parent div. The script also checks whether child node has been removed and alert some message.

<script>
function removeElement(parent, child)
{
if (document.getElementById(child)) {
var p = document.getElementById(parent);
var c = document.getElementById(child);
p.removeChild(c);
}
else
{
alert("No more child div to remove")
}
}
</script>

<div id="torque" style="border: 5px solid green; padding: 10px;background-color:green">
     This is the Parent Div.
     <div id="show" style="border: 3px solid blue; padding: 10px;background-color:yellow">
           This is Child Div
     </div>
</div>
<p>&nbsp;</p>
<input type="button" value="Remove Element" onClick="removeElement('torque','show')">

Demo

Leave a Reply

Your email address will not be published. Required fields are marked *