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

270
Views
Cannot read properties of null (reading 'addEventListener') even though element exists

I have a button in HTML that I am adding an event listener for in JavaScript.

<button class="Button" id="First" type="button">Confirm First Location</button>
map.addListener("click", (mapsMouseEvent) => {
      document.getElementById("First").style.visibility = "visible"
  }

  document.getElementById("First").addEventListener("click", function() {
    console.log("button clicked")
  });

The button is hidden by default and I want to add the event listener after the user interacts with a map. However, after the button becomes visible, I get an error saying Cannot read properties of null (reading 'addEventListener'), even though the element exists, as shown by it becoming visible.

However, if I add the event listener inside the maplistener function:

map.addListener("click", (mapsMouseEvent) => {
      document.getElementById("First").style.visibility = "visible"
      document.getElementById("First").addEventListener("click", function() {
        console.log("clicked")
      });
  }

the eventListener works. I'm confused as to why this would work in the mapListener function, and not on its own. Please let me know.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

I think u just missed a little thing...a Typographical mistakes;)

use:

map.addEventListener("click", (mapsMouseEvent) => {
    document.getElementById("First").style.visibility = "visible"
}

instead of:

map.addListener("click", (mapsMouseEvent) => {
    document.getElementById("First").style.visibility = "visible"
}
about 4 years ago · Juan Pablo Isaza Report

0

Fixed by placing scripts below the body tag

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!