What is the HTML DOM remove() method?

Overview

The remove() method removes an element from the HTML DOM. It is applied on the element which we want to remove.

Parameter

This method does not require any parameters.

Return value

This method does not return anything.

Code

Let’s write the code to remove an element from the HTML:

Explanation

  • Line 5: We have a p paragraph element with simple text.
  • Line 7: We have another p element with an elem id which is removed from the HTML page.
  • Line 9: We write a button tag with a function change() binded with the onclick event. The function is called when the user clicks on it.
  • Line 12: We write the change() function inside the script tag .
  • Line 13: We use getElementById() to get the paragraph element with the elem id and assign it in a variable.
  • Line 14: We apply the remove() method on the variable which is to be removed.

Output

When we click the button, the paragraph element with the text is removed and can not be accessible in DOM again.

Free Resources