How to run a function on onclick event in a button?
Corazone777 opened this issue · comments
Im trying to run a function on onclick event, like here https://github.com/karaxnim/karax/blob/master/tests/nativehtmlgen.nim
How can i change "javascript:myFunc()" with nim's functions, or at least include js function in a nim file?
You could create a separate module that you compile to js.
proc myFunc {.exportc.} =
echo "hello"
And load that on your page so that it runs when the user clicks the link.
It may be better though to create a karax SPA instead of using it for server side templates, depending on what you are building?
I'm just trying nim out, awesome language. I did what you suggested and got file.js, now im not sure how to include it inside file.nim so i can use that function on onclick = . I'm sorry I'm still noob.
You could add this line into your karax template to load the script:
script(`type`="text/javascript", src="./file.js")
Though your webserver will need to serve it somehow.
If the webpage you are building has a lot of interactivity I would recommend building it as an SPA (compile a karax app to js) instead of using server side templates. If you need help or advice I'd suggest joining the #webdev channel on the Nim chat channels (discord/irc/matrix etc)
I'm going to close this for now, please re-open and comment if you want to discuss this further @Corazone777