Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

209
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda