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

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

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 Denunciar

0

Fixed by placing scripts below the body tag

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