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
Is there a way to insert innerHTML of different ids using javascript

var res = document.querySelectorAll("#a,#b,#c,#d")
res[0].innerHTML = "apple"
res[1].innerHTML = "bananna"
res[2].innerHTML = "orange"
res[3].innerHTML = "lemon"
<p id="a"></p>
<p id="b"></p>
<p id="c"></p>
<p id="d"></p>

This is running fine, but I want to be able to use something like this:

var res = document.querySelectorAll("#a,#b,#c,#d")[i].innerHTML = 
  {"apple","bananna","orange","lemon"}
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Use a simple loop. I will highly recommend you to go over some simple JS tutorial so that you enjoy coding

const elements = document.querySelectorAll("#a,#b,#c,#d")
for (let i = 0; i < elements.length; i++) {
  elements[i].innerHTML = `text${i + 1}`
}
about 4 years ago · Juan Pablo Isaza Report

0

As far as data is concerned, you need at minimum a way to pick a target, a piece of content for that target, and a way to tie the target and content together. One way this might be done is:

const data = [
  ['#a', 'apple'],
  ['#b', 'banana'],
  ['#c', 'orange'],
  ['#d', 'lemon'],
];

Now that you have all the necessary data and its associations, making use of it could look like:

data.forEach(([target, content]) => {
  const element = document.querySelector(target);
  if(element) element.innerHTML = content;
});

Reference:

  • .forEach()
  • Arrow functions
  • Destructuring assignment
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!