How to generate HTML tags from Kotlin?

596 views Asked by At

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?

1

There are 1 answers

1
dilvan On BEST ANSWER

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!')"