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

153
Views
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 answers
Answer question

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 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!