delete node in javascript code example
Example 1: javascript remove dom element
var elem = document.querySelector('#some-element');
elem.parentNode.removeChild(elem);
Example 2: delete node in javascript
<ul id="shoppinglist">
<li class="complete"> Apple [<a href="#">Delete</a>] </li>
<li class="complete"> banana [<a href="#">Delete</a>]</li>
<li class="complete"> mango [<a href="#">Delete</a>]</li>
<li class="complete"> guava [<a href="#">Delete</a>]</li>
</ul>
document.getElementById("shoppinglist").addEventListener('click', function(e) {
deleteNode(e);
}, false);
function deleteNode(e) {
var target, elParent, elGrandParent;
target = e.target;
elParent = target.parentNode;
elGrandParent = elParent.parentNode;
alert(target.parentNode.innerHTML + " Removed From Cart")
elGrandParent.removeChild(elParent);
e.preventDefault();
}
Example 3: Removing DOM nodes when traversing a NodeList
NodeList nodes = ...;
for (int i = nodes.getLength() - 1; i >= 0; i--) {
Element e = (Element)nodes.item(i);
if (certain criteria involving Element e) {
e.getParentNode().removeChild(e);
}
}