javascripthtmlkotlinkotlinx-html

How to generate HTML tags from Kotlin?


I would like to generate HTML from Kotlin (running in the browser). I tried the Kotlinx library but it doesn't support callbacks, such as:

div {
    onclick = { event ->
        window.alert("Kotlin!")
    }
}

Are there other similar libraries? Maybe something similar to Clojure's Hickup or Groovy's MarkupBuilder?


Solution

  • The Kotlinx library does support callbacks. The code example in its Events Page is wrong. Just include kotlinx.html.js.* and use the event names ending with Function:

    div {
        onClickFunction = { event ->
            window.alert("Kotlin!")
        }
    }
    

    To make things more difficult, the name onClick does exist, but it takes a string: onClick = "alert('Kotlin!')"