How to remove an HTML element using JavaScript ?
Given an HTML element and the task is to remove the HTML element from the document using JavaScript.
Approach:
- Select the HTML element which need to remove.
- Use JavaScript remove() and removeChild() method to remove the element from the HTML document.
Example 1: This example uses removeChild() method to remove the HTML element.
<!DOCTYPE HTML> <html> <head> <title> How to remove an HTML element using JavaScript ? </title> <style> #GFG_DIV { background: green; height: 100px; width: 200px; margin: 0 auto; color: white; } </style> </head> <body style = "text-align:center;"> <h1 style = "color:green;" > GeeksForGeeks </h1> <p id = "GFG_UP" style = "font-size: 19px; font-weight: bold;"> </p> <div id = "GFG_DIV"> This is Div box. </div> <br> <button onClick = "GFG_Fun()"> click here </button> <p id = "GFG_DOWN" style = "color: green; font-size: 24px; font-weight: bold;"> </p> <!-- Script to remove HTML element --> <script> var up = document.getElementById('GFG_UP'); var down = document.getElementById('GFG_DOWN'); var div = document.getElementById('GFG_DIV'); up.innerHTML = "Click on button to remove the element."; function GFG_Fun() { div.parentNode.removeChild(div); down.innerHTML = "Element is removed."; } </script> </body> </html> |
chevron_right
filter_none
Output:
-
Before clicking on the button:
-
After clicking on the button:
Example 2: This example uses remove() method to remove an HTML element from the document.
<!DOCTYPE HTML> <html> <head> <title> How to remove an HTML element using JavaScript ? </title> <style> #GFG_DIV { background: green; height: 100px; width: 200px; margin: 0 auto; color: white; } </style> </head> <body style = "text-align:center;"> <h1 style = "color:green;" > GeeksForGeeks </h1> <p id = "GFG_UP" style = "font-size: 19px; font-weight: bold;"> </p> <div id = "GFG_DIV"> This is Div box. </div> <br> <button onClick = "GFG_Fun()"> click here </button> <p id = "GFG_DOWN" style = "color: green; font-size: 24px; font-weight: bold;"> </p> <!-- Script to remove HTML element --> <script> var up = document.getElementById('GFG_UP'); var down = document.getElementById('GFG_DOWN'); var div = document.getElementById('GFG_DIV'); up.innerHTML = "Click on button to remove the element."; function GFG_Fun() { div.remove(); down.innerHTML = "Element is removed."; } </script> </body> </html> |
chevron_right
filter_none
Output:
-
Before clicking on the button:
-
After clicking on the button:
Recommended Posts:
- How to remove an element from an array in JavaScript?
- How to remove HTML tags with RegExp in JavaScript?
- Set the focus to HTML form element using JavaScript
- Check whether HTML element has scrollbars using JavaScript
- How to rotate an HTML div element 90 degrees using JavaScript ?
- How to add/update an attribute to an HTML element using JavaScript?
- How to remove parent element except its child element using jQuery ?
- How to remove CSS property using JavaScript?
- How to reset/remove CSS styles for element ?
- How to remove text from a string in JavaScript?
- JavaScript | Remove the last item from an array
- JavaScript | Remove a JSON attribute
- How to remove a character from string in JavaScript ?
- How to remove all rows from a table in JavaScript ?
- How to remove time from date using JavaScript ?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



