Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

137
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!