Binding a function to a button is easy and straightforward:
<button on:click={handleClick}>
Clicks are handled by the handleClick function!
</button>
But I don't see a way to pass parameters (arguments) to the function, when I do this:
<button on:click={handleClick("parameter1")}>
Oh no!
</button>
The function is called on page load, and never again.
Is it possible at all to pass parameters to function called from on:click{}
?
I found the proper way to do it (see comments). Calling the function from an inline handler works.
<button on:click={() => handleClick("parameter1")}>
It works...
</button>
on:click{() => clickHandler(param)}
is, as Rich said above, "how it works", is because one needs to pass a reference to the function needing to be executed. By callingon:click{clickHandler(param)}
, you are executing the function immediately. That's not what you want. – Indecisive