I have the "on_event" JS function:
function on_event(event) {
console.log(event);
}
And I have the "button" HTML element builder in Rust:
let window = web_sys::window().expect("no `window` object");
let document = window.document().expect("no `document` object");
let body = document.body().expect("no `body` object");
let button = document.create_element("button")?;
// ...
body.append_child(&button);
How to link the "on_event" JS function with the "button" HTML element from the Rust code?
At the end I need something like:
button.add_event_listener("click", "on_event"); // <- What I need to use here instead of this code
For a trivial JS function I can use HTML attributes:
Also it can be used for Rust functions exported to JS with
#[wasm_bindgen]and imported to the global namespace.In Rust:
In JS:
If you are using Trunk then instead of edit of JS file you can create
Trunk.tomlfile in the root of the project folder and put this code: