Let’s first cover the current behavior, and after that, we can figure out a solution.
When using the following in a template
<%= function %>
it is calling the function and inserting the result into the template. So if your get_signals_support_count returns a value, you should see that value inside the onclick attribute after page load. This is how we populate our templates with dynamic content.
If you want to call the function, you need to somehow expose it through a Phoenix controller action or a channel (or use some third-party library like Drab). But to keep things simple let’s create a function in a Controller and expose it as an action through the router.
defmodule MyApp.SomeController do
use PaceWeb.Web, :controller
def get_signals_support_count(conn, %{"signal_id" => signal_id}) do
# do your work
end
end
And then you should be able to add a button or link to that path (get_signals_support_count_path(@conn, signal_id)
In general, whatever happens on a web page need to go through an HTTP request or a WebSocket channel. The former requires a request to be sent to the server (either with page reload or AJAX) and the latter can be done purely through javascript with Phoenix channels.
Yes, you’re right. Yoy can use it but without js event assign. From my point of view I’ll choose the first option if you don’t want to change the page.