I want to dispatch a custom event in a phoenix template, but I am getting below error
unexpected ( after alias Event. Function names and identifiers in Elixir start with lowercase characters or underscore.
What I have written to trigger the event is
<a onclick=“<%= document.dispatchEvent(new Event(“anchor”))”>
5:08
Can somebody help here?
Hi @namit-thakral, code inside of <%= %>
syntax is Elixir code. You’re trying to write Javascript code, which isn’t going to work.
If you want to do a javascript on click value simply:
<a onclick="document.dispatchEvent(new Event(\"anchor\")"
Hi @benwilson512 , I tried this
<a onclick="document.dispatchEvent(new Event(\'anchor'\))">
This was a phoenix template file, that’s why I wrote it inside <%= %>
But the error is not there anymore, but nothing is happening, after I click. The code for that is
document.addEventListener('anchor', () => {
console.log('anchor Click event triggered')
})