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

288
Views
Why is the elements text not changing?

I am trying to use JavaScript to change the text of this element. However it is not working. Here is the header im trying to change - enter image description here

var elem = document.getElementsByClassName("headertext");
elem.innerHTML = "hello world";
<ul class="headertable">
  <li>
    <p class="headertext">Home</p>
  </li>
  <li>
    <p class="headertext">About US</p>
  </li>
  <li>
    <p class="headertext">Contact US</p>
  </li>
</ul>

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

0

document.getElementsByClassName returns an array-like object of all child elements which have all of the given class name(s).

Here the elem will be an HTMLCollection. So elem.innerHTML is not valid.

So you should use the index to update the required node

like elem[0].innerHTML or elem[1].innerHTML or elem[2].innerHTML

var elem = document.getElementsByClassName("headertext");
elem[0].innerHTML = "hello world";
elem[1].innerHTML = "hello world 1";
elem[2].innerHTML = "hello world 2";
<ul class="headertable">
    <li><p class="headertext">Home</p></li>
    <li><p class="headertext">About US</p></li>
    <li><p class="headertext">Contact US</p></li>
</ul>

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!