0

I want the div to be output in the console but I am getting 'null' in the console. This is my HTML ->

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script src="two_js.js"></script>
    <title>:)</title>
</head>
<body>
    <div id="main" class="container">
        This is my HTML Body.
    </div>
</body>
</html>

This is my JavaScript ->

let main = document.getElementById('main');
console.log(main);

What should I do?

1 Answers1

0

Put your script tag inside and at the bottom of body tag.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
   
    <title>:)</title>
</head>
<body>
    <div id="main" class="container">
        This is my HTML Body.
    </div>

<script src="two_js.js"></script>
</body>
</html>
Layhout
  • 1,445
  • 1
  • 3
  • 16