Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kotlin get target of jquery click

Tags:

kotlin

If I write the next code I get the compiler error: "Unresolved refrence:target"

jq("#element").click {
    console.log(it.target)
}

However, if I print only "it" it has the property target

r.Event {originalEvent: MouseEvent, type: "click", target: button, currentTarget: button, relatedTarget: null…}

How I'm supoused to get the target then?

like image 538
eloo Avatar asked Mar 09 '17 14:03

eloo


1 Answers

I suppose you use jq from standard library, and first of all jq from standard library is deprecated.

Then lets look at definition of click handler:

public fun click(handler: (MouseClickEvent) -> Unit): JQuery

As you see, it in your case is MouseClickEvent. But MouseClickEvent and MouseEvent doesn't contains target.

You can write own bindings for jquery:

import jquery.MouseClickEvent
import jquery.MouseEvent

@JsName("$")
public external fun jq(selector: String): JQuery

public external class JQuery() {
    public fun click(handler: (ExtendedMouseClickEvent) -> Unit): JQuery
}

public external class ExtendedMouseClickEvent() : MouseEvent {
    public val target: JQuery
    public val which: Int
}

fun main(args: Array<String>) {
    jq("#element").click {
        console.log(it.target)
    }
}

Also, you can convert existing definitions for TypeScript to kotlin.

jQuery typings: https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/jquery

ts2kt: https://github.com/Kotlin/ts2kt

like image 106
Ruslan Avatar answered Jan 03 '23 16:01

Ruslan