Home > OS >  Why is my document.querySelector returning null?
Why is my document.querySelector returning null?

Time:12-04

Despite putting the document.querySelector in a window.addEventListener("load"), my document.querySelector on the "sign-in-box" returns null. Here is my HTML:

<!DOCTYPE html>
<html lang="fr">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Accueil</title>
        <script src="dist/index.js"></script>
        <link rel="stylesheet" href="css/global.css">
        <link rel="icon" href="img/favicon.png" type="image/png" />
    </head>
    <body>
        <div class="sign-in-box">
            <form action="" method="post" autocomplete="off">
               <div class="sign-in-input-group">
                    <div id="api-message"></div>
                    <h1>Chadbox™</h1>
                    <input type="text" name="username" placeholder="Nom d'usager" required 
                     id="usernameBox">
                    <input type="password" name="password" placeholder="Mot de passe" required 
                     id="passwordBox">
                    <button id="connectButton">Connexion</button>
                    <a href="register.html" id="registerButton">S'enregistrer</a>
                </div>
            </form>
        
        </div>
        <div id="container"></div>
        <div id="startButton">Start</div>

    </body>
</html>

and here is the relevant part of my Javascript:

window.addEventListener("load", () => {
document.querySelector("form").onsubmit = function () {
    return signin(this);
}
new Vue({
    el: '#container',
    components: { carSelector },
    template: '<carSelector/>'
})

let startBtn = document.querySelector("#startButton");
startBtn.addEventListener('mouseover', () =>{
    startBtn.style.backgroundImage = "url('img/checkered_flag.gif')";
    startBtn.style.border = "4px solid red";
})
startBtn.addEventListener('mouseleave', () =>{
    startBtn.style.backgroundImage = "url('img/flag.png')";
    startBtn.style.border = "4px solid teal";
})
startBtn.addEventListener('click', () =>{
    window.requestAnimationFrame(moveStartBox);
    let audio = document.createElement('audio');
    audio.src= "./sounds/menumusic.mp3";
    audio.loop = true;
    audio.play();
    appearLoginBox();
    let node = document.querySelector("#sign-in-box");
    console.log(node);                                        //<- returns null
})

I had a similar problem with getting audio to play, which I managed to make works by just creating a new element, but I can't do that in this case. Is it because I have put the querySelector in an EventListener?

CodePudding user response:

document.querySelector("#sign-in-box");

targets the id of the element so you should either update the JavaScript selector

document.querySelector("#sign-in-box");

to

document.querySelector(".sign-in-box");

or update the html to

 <div class="sign-in-input-group">

to

<div id="sign-in-input-group">
  • Related