Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

185
Visualizações
Second <span> in JS not working "Uncaught TypeError: Cannot set properties of null (setting 'onclick')"

i wrote a JS code for my Website, which should show a modal and close it, if i click the "close" span. The first time, it worked perfectly. Here is the code:

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";
} 

But the second time, the close button just dont work and i get

"Uncaught TypeError: Cannot set properties of null (setting 'onclick')"

Here is the second code piece:

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";
}

I think i can't use two spans, but what else should i do? Thank you very much!

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

The second time img is null, so you cannot set img.onclick. The reason it is null is probably that there is no element with id showstartmenu, so when you are trying to get it (document.getElementById("showstartmenu")), you are getting null instead.

Edit:

Sorry, it could also be that span is null, for the same reason. I missed that one.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda