Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

148
Visualizações
Why does addEventListener function showing the error?

I faced this problem when i doing my project and not able to use addEventListener function. So I make new file and try to catch the error, so basically its store null value to the variable that's why i am not able to use. When i use the same code on the console , that was executed and give the expected result but not with VS code.

*JS code

let elem7=document.getElementById("mainDiv")
console.log(elem7)//prints null

elem7.addEventListener('click',function(e){
    console.log("You Clicked")
})  

HTML CODE:

<body>
    <h3>Editable div</h3>
    <div id="mainDiv">

    </div>
</body>
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Just add your id tag to your <h3> element.

Your <div> is empty, and because of that, it is not clickable.

let elem7=document.getElementById("mainDiv")
    console.log(elem7)//prints the whole HTML element
    
    elem7.addEventListener('click',function(e){
        console.log("You Clicked")
    })  
    <body>
        <h3 id="mainDiv">Editable div</h3>
        <div>
    
        </div>
    </body>

about 4 years ago · Juan Pablo Isaza Relatório

0

Try this and see console, it might works!

let elem7=document.getElementById("mainDiv")
console.log(elem7)//prints null

elem7.addEventListener('click',function(e){
    console.log("You Clicked")
})  
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="src/style.css">
  </head>
  <body>
    <h3>Editable div</h3>
    <div id="mainDiv">
      Click me
    </div>
  </body>
</html>

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda