2619 2619 - 11 months ago 82
Javascript Question

Get all child elements including body using javascript

The code below gets all children within

but it does not get the
element itself. How can I get all child elements including
with pure JavaScipt?

document.querySelectorAll('body *');

Answer Source

You are getting all inner elements of body tag instead use * to get all elements.


If you want to get body and its all inner elements then use comma separated multiple selectors.

document.querySelectorAll('body,body *');