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

147
Vistas
How to create DOM element with click handler in one go?

I want to create an element on the fly, like this way:

var productItemTop = $(
  "<span>" +
    "<a class='spamItemsX' href='#' " +
    "onclick=" +
    eval(launchGenericProductSearch(topProducts)) +
    ">" +
    topProducts +
    "</a>" +
    "</span>"
);

But every time I load the page, the function launchGenericProductSearch is get called, but I don't want it to be called then, but when the link is clicked.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Indeed, launchGenericProductSearch will be executed immediately, not when the user clicks.

You will get better control over your code when you use jQuery to the full, avoid eval, and bind click handlers not via HTML, but via JS (jQuery) code:

var productItemTop = $("<span>").append(
    $("<a>").addClass('spamItemsX')
           .attr("href", '#')
           .click(() => launchGenericProductSearch(topProducts))
           .text(topProducts)
    );
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