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

166
Views
Put variable in getelementsbyclassname.innertext but not work?

So I have this text

" This is Sam.

Sam is my friend.

Sam is ten years old.

Sam likes football."

You can see Sam is repeated so many times. I want to assign it to a variable so that when I need to I can change the character name. Here is how I write it

var name = "sam"
document.getElementsByClassName("name").innerText= name;
<p> This is <span class="name"></span>.</p>
<p> <span class="name"></span> is my friend</p>
<p> <span class="name"></span> is ten years old.</p>
<p> <span class="name"></span> likes football.</p>
However, it didn't work. I also use document.querySelectorAll("span.name").innerText= name; but still didn't work.

Please help me.

Thank you.

about 4 years ago · Juan Pablo Isaza
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!