kotlinx.html icon indicating copy to clipboard operation
kotlinx.html copied to clipboard

Attaching events thru kotlin-html does not work

Open StokeMasterJack opened this issue 7 years ago • 3 comments

This code:

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

On this doc page: https://github.com/kotlin/kotlinx.html/wiki/Events

No longer works.

StokeMasterJack avatar May 11 '17 17:05 StokeMasterJack

I believe it should be onClickFunction instead of onclick:

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

StokeMasterJack avatar May 11 '17 20:05 StokeMasterJack

Yes, you are completely right: onclick is a string property

cy6erGn0m avatar May 13 '17 15:05 cy6erGn0m

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
            ...
        }

kosiakk avatar Sep 04 '17 14:09 kosiakk