kotlinx.html
kotlinx.html copied to clipboard
Attaching events thru kotlin-html does not work
This code:
div {
onclick = { event ->
window.alert("Kotlin!")
}
}
On this doc page: https://github.com/kotlin/kotlinx.html/wiki/Events
No longer works.
I believe it should be onClickFunction instead of onclick:
div {
//not
onClickFunction = { event ->
window.alert("Kotlin!")
}
}
Yes, you are completely right: onclick is a string property
Btw, Kotlin.DOM
has an interface org.w3c.dom.events.EventListener
, which declares a single function:
fun handleEvent(event: Event): Unit
However, all those onClickFunction
accept (Event) -> Unit
, which is the same, in priciple.
So, if you have an instance of EventListener
, you can pass it to the DOM handler by qualified ::
reference:
import kotlinx.html.*
import kotlinx.html.js.*
import org.w3c.dom.HTMLFormElement
import org.w3c.dom.events.EventListener
import kotlin.browser.document
fun loginForm(listener: EventListener): HTMLFormElement =
document.create.form {
onSubmitFunction = listener::handleEvent
...
}