Hey guys,
I'm trying to call two functions using the onclick attribute like so:
<script src="main.js"></script> <button onclick="addCompetitor()">Add Competitor</button> <button onclick="delCompetitor(1)">Del Competitor</button>
These functions are defined in a JavaScript file generated by Nim.
when serving my page, I keep getting errors when I click either button saying they're undefined:
Uncaught ReferenceError: addCompetitor is not defined
at HTMLButtonElement.onclick ((index):42)
I understand the JavaScript Nim creates is solely meant for the browser, but is there a way to achieve the functionality I want like the code above? Or am I forced to try to do something like add event listeners in the JavaScript file?
You can use the exportc pragma to have Nim export the proc non mangled into a js file as follows.
proc add(a, b: int): int {.exportc.} = a + b
When using nim js -d:release it outputs this js function
function add(a_385875971, b_385875972) {
var result_385875973 = 0;
result_385875973 = addInt(a_385875971, b_385875972);
return result_385875973;
}
I found 1 issue, I wasn't using the {.exportc.} pragma (still a noob with any of the FFI stuff).
I'm still not able to call the second proc, the one passing the param. Here is the proc:
nim
proc delCompetitor(divIdNum :cint) {.exportc.} =
echo "deleting competitor"
if divIdNum != 1:
var competitor = document.getElementById("competitor-" & $(divIdNum))
competitor.remove()
else:
echo "cannot delete first competitor"
Thanks man, didn't see your reply during my last comment here.
Amazing.