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

198
Visualizações
Code stops working when I add a function call but works when I remove the call

This code works. It changes the button color without any problem


   document.addEventListener("keypress", function(event) {
        if (event.keyCode == 49) {
     

      button1.style.backgroundColor = "lightgreen";
     
      setTimeout(() => {
       button2.style.border = "";
       button1.style.backgroundColor = "";
          
          console.log("changed style ")

      }, 100);
      getDate();
   }

But when I add socket.Emit("hello","world") It throws the error "getting TypeError: Cannot read properties of null (reading 'style')" it can no longer change the color of button1.


  document.addEventListener("keypress", function(event) {
        if (event.keyCode == 49) {
      socket.emit("hello" , "world") <= When this is added

      button1.style.backgroundColor = "lightgreen"; <= Error is caused by this line
     
      setTimeout(() => {
       button2.style.border = "";
       button1.style.backgroundColor = "";
          
          console.log("changed style ")

      }, 100);
      getDate();
   }
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Button1 is not being defined thus it cannot apply a style to it. Please provide more code or look back and properly define it.

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