Genie Discord forum

Author AvatarJuliane Müller-Sielaff
2/24/2023, 8:10:32 AM

Hello, I'm currently implementing a webpage using Genie. I have a button on my webpage and as soon as it gets pressed a julia function shall be executed.

The code and html looks similar to this:

`values = Dict(("A", 1), ("B", 2))

"<button class="add_button" onclick=$(interactively_add_view_element(values))> + ")

function interactively_add_view_element(values::Dict()) println(values"A") end`

However, the function is called when loading the page and not on the click event.

Author AvatarPere
2/25/2023, 7:51:10 PM

The reason why the code is executed when the page loads is because code interpolations with $(...) and <% ... %> are executed on load.

One way to trigger a function when a button is pressed is by binding it to a variable and executing code when the variable changes. Like this:

using GenieFramework

vdict = Dict([("A", 1), ("B", 2)])

function interactively_add_view_element(vd)

@handlers begin
    @in btn = false
    @onchange btn begin

<button class="add_button" v-on:click='btn = !btn'> +</button>
Author Avatarhhaensel
3/7/2023, 11:43:24 PM

the syntax is btn("Label", icon = "mail", @click(:btn))

Author Avatarhhaensel
3/7/2023, 11:45:26 PM

It might be a problem that btn is a fieldname and a function name, but I think that this case is already covered. If it is not working try renaming the button field to :mybutton