1//removing element by ID
2var element = document.getElementById("myElementID");
3 element.parentNode.removeChild(element);
1//remove a dom element
2var element = document.getElementById("someId");
3 element.parentNode.removeChild(element);
4
5//remove element from array
6var colors=["red","green","blue","yellow"];
7var blue=colors.splice(2, 1);//first arg is array index to remove
8//colors is now ["red","green","yellow"]
9
1Removing an element is much easier, as it only requires the element's ID.
21. function removeElement(elementId) {
32. // Removes an element from the document.
43. var element = document. getElementById(elementId);
54. element. parentNode. removeChild(element);
65. }
7
8Example:
9<h1>The remove() Method</h1>
10
11<p>The remove() method removes the element from the DOM.</p>
12
13<p id="demo">Click the button, and this paragraph will be removed from the DOM.</p>
14
15<button onclick="myFunction()">Remove paragraph</button>
16
17<script>
18function myFunction() {
19 var myobj = document.getElementById("demo");
20 myobj.remove();
21}
22</script>