onclick function in Template literals inside a clickable div is not working

  javascript, reactjs

So I need to be able to do this, But the onClick function is not recognized as a function, but as a string. The output is

{ e.stopPropagation(); e.preventDefault() history.push(‘/profile/test’) }}>@test

with @test being the ${newmes}.

“` newText=testmessage.replace(/@[a-z]+/i,`<p onClick={(e)=>{

 e.stopPropagation();
 e.preventDefault()
 history.push(x27/profile/${username}x27)}>${newmes}</p></strong>`)

let texte=document.getElementById(‘posttext’)

texte.innerHTML=newText“`

And if I just want to call a function inside the onClick, like this: onClick={handleclick}. Then I get an error saying "handleclick is not defined at HTMLParagraphElement.onclick".

So how can I fix this? I need to this because I want the woord beginning with a @ to be a link inside a string. And that string is inside a div thats also clickable, thats why I need to use e.stopPropagation();
. So if I just use a then the links works fine, but because it is inside a clickable div I need to have a onClick function I think.

Im sorry for my bad english, I hope someone has an answer.

Source: Ask Javascript Questions

LEAVE A COMMENT