How to add onclick method using js_of_ocaml?

Hi, I have a button with id form-submit, and I have the following function in my OCaml file:

let () =
   let form_submit = Dom_html.getElementById "form-submit" in
   ...

What is the correct way to add an on_click method to a button? Should I just do something like this?

  form_submit##onclick <- ...

But what is the type of handler function? I can not find it in the documentation.

+4
source share
1 answer

You can use dom material manually, but this is not very convenient. The right way is to use Lwt_js_eventsmodules that create an abstraction over event handlers using Lwt.

Here is a minimal example:

Lwt.async (fun () ->
  Lwt_js_events.clicks form_submit
    (fun _ev _thread -> Lwt.return () )
  )

The first parameter is the event object, the second is the loop thread. You can fine-tune the behavior of the handler using the general construction at the beginning.

Lwt.async , " " ( , lwt-). , , .

+5

All Articles