How to attach OCaml values to DOM elements?

It is often convenient to attach OCaml values to some elements of the page. There are several ways to do that.

  • The first possibility is to use DATA attributes (for example if the page is generated on server side.
  • Or you can add a new property to the element (using the ## syntax)
    • either you create a new class type with the new property (that can be either an OCaml value or a JS value) (possibly inheriting from the type of the DOM element) and create an (unsafe) coercion function for this type
    • or just create getter and setter functions to add the new property