Js_of_ocaml: How to retrieve input value in the event handler of input element

Hi,

I am trying to retrieve the input value from the code below via ```

  let i_elt = Html.(input ~a:[ a_id "input1" ] ()) in
  let i_dom = To_dom.of_input i_elt in
  i_dom##.oninput
    := Dom_html.handler (fun evt ->
           (* doesn't work*) 
           (* Js.Opt.iter evt##.target (fun t -> log t##.value); *)
           Js.Opt.iter evt##.target (fun tgt -> log t##.id);
           Js._true);

It seems tgt object is an instance of an Dom_html.element rather than the Dom_html.inputElement, is there a way to retrieve the text value entered in input element. I have tried innerHTML and textContent, but no luck. Both return empty string.

1 Like

You don’t seem to be trying to do anything with tgt, but if that is a typo, try applying Dom_html.CoerceTo.input to it and examining its value field.

Thanks. Yep. That works.