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

89
Views
how do I get an element out from the forEach loop

Please tell me how do I get that AllMainSongsIndex out from the loop and how do I use it

songsItems.forEach((element, i) => {
      let Allcovers = element.querySelectorAll(".cover")
      Allcovers[0].src = songs[i].coversPath
      let AllNames = element.getElementsByClassName("Names");
      AllNames[0].innerHTML = songs[i].songName;
      let AllMainSongsIndex=element.querySelector(".currentsoundGIF").id;
      return AllMainSongsIndex;
});

Thank you in advanced

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

0

Define it outside the loop, assign inside the loop and use it outside the loop. But it's confusing about which one you want to use, may be use some logic to use a particular value, in your case the last loop element will get assigned.

let AllMainSongsIndex= "";

songsItems.forEach((element, i) => {
      let Allcovers = element.querySelectorAll(".cover")
      Allcovers[0].src = songs[i].coversPath
      let AllNames = element.getElementsByClassName("Names");
      AllNames[0].innerHTML = songs[i].songName;
      AllMainSongsIndex=element.querySelector(".currentsoundGIF").id;
});
// Use it here
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!