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

154
Visualizações
getElementById not working in class method

HTML:

<div id='verse'>
    <p id='text'>
        <span id = 'reference'>random stuff here</span> 
     random stuff here
     </p>
  </div>

JS:

class Verse {
  ...
  update() {
    document.getElementById('text').innerHTML = 'text';
    document.getElementById('reference').innerHTML = 'reference';
  }
  ...
}

Whenever I call .update in an instance of Verse, the paragraph element's text changes without a problem but trying to change the span's text gives me an error: TypeError: Cannot set properties of null. Does not work with innerText either. It works fine if I change it outside of the class. Thanks for the help!

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

0

Setting innerHTML on #text replaces the contents of the <p> entirely, so when you subsequently try to access #reference it no longer exists.

The simplest way to avoid this would be to add another span and replace its text instead of the entire <p>.

<div id='verse'>
  <p>
    <span id='reference'>random stuff here</span> 
    <span id='text'>random stuff here</span>
  </p>
</div>
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