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

135
Views
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 answers
Answer question

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 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!