Remove the last node from its parent in JavaScript

Description

The following code shows how to remove the last node from its parent.

Example


<!DOCTYPE html>
<html>
<body>
<div id="div1">
<ul>
<li>List item 1</li>
<li>List item 2</li>
<li>List item 3</li>
</ul><!--from w  w  w  .j a  v  a 2  s . c om-->
</div>
<p id="p1">Hello <b>World!</b></p>


<script type="text/javascript">
var div = document.getElementById("div1");
var p = document.getElementById("p1");


var formerLastChild = div.removeChild(div.lastChild);
document.writeln(formerLastChild);

</script>
</body>
</html>

Click to view the demo

The code above generates the following result.

Remove the last node from its parent in JavaScript
Home »
  Javascript Tutorial »
    DOM »
      Delete
Javascript Tutorial Delete
Remove a child element from a HTML element ...
Remove node from another node in Javascript...
Remove the last node from its parent in Jav...