How do you attach functionality to buttons in Embedded Elixir?

175 Views Asked by At

I'm rendering templates like so:

<%= for country <- @countries do %>
  <div>
    <div>A list of countries you have visited</div>
    <a href="/countries/<%= String.downcase(country) %>"><%= country %></a>
    <button> Click This </button>
  </div>
<% end %>

in js of course I can do <button onclick={myFunction}/> but how do I do this in template world in elixir?

1

There are 1 best solutions below

2
Yatender Singh On BEST ANSWER

Phoenix provides a lot of binding events which you can utilise in the phoenix based apps.

For e.g. if you want to call a function on click of a button then use the phx-click to call the function in the elixir module.

You can read more about the bindings.

https://hexdocs.pm/phoenix_live_view/bindings.html