0

In the following code, alert(el) outputs null and I don't understand why this is happening. If I write just querySelector('div') everything is working.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script src="./jquery.js"></script>
    <title>Document</title>
    <style>
        
        div {
            width: 200px;
            height: 200px;
            border: 1px solid black;
            background-color: blue;
        }

    </style>
</head>
<body>
    <p>
        <div>
            Good bye
        </div>
    </p>
    <script>

        let el = document.querySelector("p div");
        alert(el);

    </script>
</body>
</html>
AndrewL64
  • 15,794
  • 8
  • 47
  • 79
Daler
  • 5
  • 3
  • 4
    That's because there is no `div` inside a `p` in your document. _"But why? You can clearly see it in my HTML"_ Yes, but your HTML is invalid, so the browser fixed it for you. A `

    ` cannot contain a `

    `. Right click on "Good bye" and choose "Inspect", you'll see what your browser did to fix it
    – blex Jul 05 '20 at 08:53
  • You really helped me. Thank you very much and sorry for my stupid question I should have seen HTML DOM in the browser but I didn't. – Daler Jul 05 '20 at 08:59

0 Answers0