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

137
Views
How to add an onClick function on an anchor tag wrapped in a string?

I have an anchor tag wrapped in a string.

export const linkify = (inputText) => {
    let replacePattern = /(\b(https?|ftp):\/\/[-A-Z0-9+&#\/%?=~_|!:,.;]*[-A-Z0-9+&#\/%=~_|])/gim;
    if (replacePattern.test(inputText)){
        inputText=inputText.replace(replacePattern, function(url) {
            return `<a href="${url}" onClick=${() => onLinkClick()}>${url}</a>`;
        })
    }
    return inputText;
}

const onLinkClick = () => {
    // do something
    console.log('clicked');
}

I want to add an onClick on the anchor tag which I did but it is not working

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

create element and edit it's attribute.

const onLinkClick = () => {
  // do something
  console.log("clicked");
};

export const linkify = (inputText) => {
  let replacePattern =
    /(\b(https?|ftp):\/\/[-A-Z0-9+&#\/%?=~_|!:,.;]*[-A-Z0-9+&#\/%=~_|])/gim;
  if (replacePattern.test(inputText)) {
    inputText = inputText.replace(replacePattern, function (url) {
      const a = document.createElement("a");
      a.href = url;
      a.innerText = url;
      a.addEventListener("click", onLinkClick);

      return a;
    });
  }
  return inputText;
};

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!