To remove an element using javascript, you can use one of the following snippets.

Sample Javascript

//usage document.getElementById("ElementId").remove();
Element.prototype.remove = function() {
	this.parentElement.removeChild(this);
}

Sample Javascript

//usage document.getElementsByClassName("Elements").remove();
NodeList.prototype.remove = HTMLCollection.prototype.remove = function() {
	for(var index = 0, length = this.length; index < length; index++) {
		if(this[index] && this[index].parentElement) {
			this[index].parentElement.removeChild(this[index]);
		}
	}
}

removeChild() is the only method supported by the DOM to remove a element in Javascript (see -> Mozilla MDN Node.removeChild()).

9 thought on “How to remove an element using Javascript”

Leave a Reply