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

115
Views
How to assign the variables of an array to elements in querySelectorall [Pure Javascript]

I am trying to assign the values of an array to elements that were selected by the querySelectorAll function, Any help is appreciated. The code I am trying to write that when the viewpoint of the browser is below 768px, the text changes to the ones in the array, I have tried all the methods I could think of

var newb = document.querySelectorAll("#buttonsize button")
var arSizes = ["Small", "Medium", "Large", "X-Large", "Large", "XX-Large"]
    for(var i = 0; i < arSizes.length; i++) {
      newb[i].textContent = arSizes[i]
   }

It gives this error

Uncaught TypeError: Cannot set properties of undefined (setting 'textContent')

I also tried doing newb.textContent and also using a forEach function.

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

0

I think you haven't enough buttons in html so better listing or buttons firstly

var newb = document.querySelectorAll("#buttonsize button");
var arSizes = ["Small", "Medium", "Large", "X-Large", "Large", "XX-Large"];

Array.from(newb).forEach((btnEl, index) => {
  btnEl.textContent = arSizes[index]
});
<div id="buttonsize">
  <button>1</button>
  <button>2</button>
  <button>3</button>
  <button>4</button>
  <button>5</button>
  <button>6</button>
</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!