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
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;
};