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

187
Views
For loop only displaying last iteration Javascript

I'm creating a for loop that adds a button to every link on the page. However, it's only adding the button to the last link of the array.

// Vars
let links = document.querySelectorAll(".g a");
let dropdownBtn = document.createElement("button");

// Call
CreateBtn();
StyleLinks();

function CreateBtn() {
  dropdownBtn.id = "dropdown-btn";
}

// Style and loop links
function StyleLinks() {
  let count = 0;
  for (let i = count; i < links.length; i++) {
    links[i].style.color = "red";
    links[i].appendChild(dropdownBtn);
  }
}

about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Your code is creating 1 button and appending it to the tags, you need to create a new instance of the button every single time the loop runs.

about 4 years ago · Santiago Trujillo 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!