Remove body element

How to remove HTML <body> element with all content?

 var e = document.getElementsByTag('html'); e.removeChild('body'); 

Does not work.

+4
source share
5 answers
  • getElementsByTagName returns a collection of nodes, not a single node
  • removeChild accepts a node, not a string containing the tag name
  var e = document.body;
     e.parentNode.removeChild (e);

... however, HTML documents require a body element, so this may have unexpected behavior.

+10
source

A simple solution would be

  document.body.innerHTML = ""; 

But why do you want to do this?

By the way:

  var e = document.getElementsByTag('html'); 

it should be

  var e = document.getElementsByTagName('html')[0]; 

and

  e.removeChild('body'); 

it should be

  e.removeChild(document.body); 
+8
source

...

 document.body.parentNode.removeChild(document.body); 
+4
source

document.body.parentNode.removeChild(document.body)
or
document.body = document.createElement("body")
or
while(document.body.childNodes.length != 0) document.body.removeChild(document.body.childNodes[0])

+3
source

I think it will delete him

 var html = document.getElementByTagName('html')[0]; var body = document.getElementByTagName('body')[0]; html.removeChild(body); 
+2
source

Source: https://habr.com/ru/post/1310774/


All Articles