js-of-ocaml

Use the value of a textarea with js_of_ocaml


I'm trying to use the value of a textarea with js_of_ocaml. Here is my source code :

let matrix () =
  let text1 = get_textarea "input_1" in
  let btn1 = get_elem "btn_1" in
  text1##placeholder <- Js.string "Write your matrix !";
  btn1##textContent <- Js.some (Js.string "Calculate");
  btn1##onclick <- Dom_html.handler (if Matrix.is_matrix (Js.to_string text1##value) 
                                     then (let matrix = Matrix.get_matrix (Js.to_string text1##value) in
                                       fun ev -> (set_matrix_caracteristics (Matrix.nb_lines matrix) (Matrix.nb_columns matrix) (Matrix.determinant matrix) ; Js._false))
                                     else fun ev -> (error_input_matrix() ; Js._false))

I want to do some calculs on a matrix, and the matrix is write by the user through a textarea with an html interface.

I think the problem is that the value of text1don't change, even if I write something in the textarea. Whatever the input on the textarea is, the result is the same.

Do anyone know how to use the value write by the user ?

Thanks !


Solution

  • Your probleme here is that you evaluate text1##value before defining the handler function, so the value is evaluated when the handler is installed, and then never changed. the following should works

       btn1##onclick <- Dom_html.handler (
           fun ev -> 
                    if Matrix.is_matrix (Js.to_string text1##value) 
                    then let matrix = Matrix.get_matrix (Js.to_string text1##value) in
                                        (set_matrix_caracteristics (Matrix.nb_lines matrix) (Matrix.nb_columns matrix) (Matrix.determinant matrix) ; Js._false)
                    else error_input_matrix() ; Js._false
          )