Why does my getElementById() not work!

Asked

Viewed 373 times

0

I am trying to get a specific paragraph of my html document.

<!DOCTYPE html>
<html>
    <head>
        <link href="style.css" rel="stylesheet">
        <script src="script.js"></script>
        <title>Documento</title>
    </head>
    <body>
        <h1>Titulo</h1>
        <p class="Paragrafo" id="ponto">Paragrafo 1</p>
        <p class="Paragrafo" >Paragrafo 2</p>
        <p id="p3">Paragrafo 3</p>
    </body>
</html>

Through your id (id = "dot"), using javascript.

let p1 = document.getElementById("ponto")
console.log(p1) 

But for some reason he always returns to me null

inserir a descrição da imagem aqui

  • If you arrow the id would not even need to do this function... just call ponto

  • 8

    You are trying to select an element that does not yet exist in the DOM, because you loaded the JS at the beginning of the file, before the elements; for this reason it is recommended to put the JS files at the end of the HTML.

  • involves the js code in window.onload = function () { /* codigo aqui */ };

  • 2

1 answer

4


Hello,

As some friends have already said here, you are trying to catch an element that does not yet exist DOM, because what is inside the tag body shall be executed only after the execution of the elements of head. There are some simple solutions to your problem. The first (but not widely used) is to tag script after body. See:

<!DOCTYPE html>
<html>
    <head>
        <link href="style.css" rel="stylesheet">
        <title>Documento</title>
    </head>
    <body>
        <h1>Titulo</h1>
        <p class="Paragrafo" id="ponto">Paragrafo 1</p>
        <p class="Paragrafo" >Paragrafo 2</p>
        <p id="p3">Paragrafo 3</p>

        <script src="script.js"></script>
    </body>
</html>



Another solution is to make your javascript code wait for the creation of window in the DOM, using the window.onload. This solution is more archaic than the following. See:

window.onload = function(){
    let p1 = document.getElementById("ponto")
    console.log(p1);
};



The most "up-to-date" solution is using the DOMContentLoaded, an event triggered when all HTML is created and analyzed, without waiting for Images, CSS and others to load. See:

document.addEventListener("DOMContentLoaded", function(event){
    let p1 = document.getElementById("ponto")
    console.log(p1);
});



I hope I helped, hug!

  • 3

    Lucas, where did the information come from that put the script at the end of the body is less used, or not "as technical"? And how about using the event DOMContentReady with HTMLElement.addEventListener(), would not be a better solution than using the archaic window.onload?

  • 1

    Hello Fernando, javascript runs synchronously, and put it in the tag head ensures that it is executed before any element of the body be created. Yes, the DOMContentReady is really more appropriate, because the load should be used only to detect when the page is fully loaded. Very well remembered!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.