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

212
Views
jQuery prepend() stringifies argument

I have the following code:

sections=$("section");
for(let i=0; i<sections.length; i++){
    let sectionTempCode=("<button id='").concat(i.toString()).concat("'>Toggle section view</button>");
    console.log(sectionTempCode)
    sections[i].prepend(sectionTempCode)
}

The intention is to add button tags to the HTML and display a button in the beginning of each . Instead, what I get is the code for the tag inside double quotes, which results in the code itself being displayed in my page:

"<button id='0'>Toggle section view</button>"
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Use .each() and the Elements generator $(HTMLTag, {...properties})

$("section").each((i, el) => {

  $("<button>", {
    prependTo: el,
    id: i+1,
    type: "button",
    text: "Toggle section view",
    click() {
      console.log(this.id);
    }
  });

});
<section></section>
<section></section>
<section></section>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>

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!