When removing an element with standard JavaScript, you must go to its parent first:
var element = document.getElementById("element-id");
element.parentNode.removeChild(element);
Having to go to the parent node first seems a bit odd to me, is there a reason JavaScript works like this?
You could make a
remove
function so that you wouldn't have to think about it every time:you can just use
element.remove()
It's what the DOM supports. Search that page for "remove" or "delete" and removeChild is the only one that removes a node.
According to DOM level 4 specs, which is the current version in development, there are some new handy mutation methods available:
append()
,prepend()
,before()
,after()
,replace()
, andremove()
.http://red-team-design.com/removing-an-element-with-plain-javascript-remove-method/