Javascript Get the Body Element

<script>   
    let body = document.body;
</script>

Getting the body of the document using javascript is very simple:

<script>   
    let body = document.body;
</script>

that will give you the Body element. You may also get it another way by running:

<script>

    let body = document.getElementsByTagName('body')[0];

</script>
```

However, if you use the second method you should either put the javascript at the end of the document or you'll need to load it inside of a `window.load` function like so:

```
<script>window.onload = function get_body() {
        let body = document.getElementsByTagName('body')[0];
    }

</script>

The easiest way is to call it directly from the document using document.body

BETA Snippet explanation automatically generated by OpenAI:

No explanation generated yet.

Snippet By Dev Dojo

·

Created June 12th, 2021

·

Report Snippet