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

178
Views
El segundo <span> en JS no funciona "Error de tipo no detectado: no se pueden establecer propiedades de nulo (estableciendo 'onclick')"

Escribí un código JS para mi sitio web, que debería mostrar un modal y cerrarlo, si hago clic en el lapso "cerrar". La primera vez, funcionó perfectamente. Aquí está el código:

 var modal = document.getElementById("notifications"); var img = document.getElementById("shownotificationbar"); var span = document.getElementsByClassName("close")[0]; img.onclick = function() { modal.style.display = "block"; } span.onclick = function() { modal.style.display = "none"; }

Pero la segunda vez, el botón de cerrar simplemente no funciona y obtengo

" TypeError no detectado: no se pueden establecer las propiedades de nulo (configurando 'onclick') "

Aquí está la segunda pieza de código:

 var menu = document.getElementById("startmenu"); var img = document.getElementById("showstartmenu"); var span = document.getElementById("closemenu"); img.onclick = function() { menu.style.display = "block"; } span.onclick = function() { menu.style.display = "none"; }

Creo que no puedo usar dos tramos, pero ¿qué más debo hacer? ¡Muchos gracias!

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

0

La segunda vez que img es nulo, por lo que no puede configurar img.onclick . La razón por la que es null es probablemente que no hay ningún elemento con id showstartmenu , por lo que cuando intenta obtenerlo ( document.getElementById("showstartmenu") ), obtiene null en su lugar.

Editar:

Lo sentimos, también podría ser que el span sea nulo, por la misma razón. Me perdí ese.

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!