2619 2619 - 2 months ago 12
Javascript Question

Get all child elements including body using javascript

The code below gets all children within

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

document.querySelectorAll('body *');

Answer

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

document.querySelectorAll('*');

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

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