Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

138
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda