Why can't I pass closure here, to JQuery click()?


#1

This syntax usually works, but I'm getting an compilation error:

  import js.jquery.*a
  …   
  jq(button).click { e -> console.log(e) }  // error here

The error is:

  Error:(59, 16) Kotlin: None of the following functions can be called with the arguments supplied:
  public final fun click(handler: org.w3c.dom.Element.(js.jquery.MouseClickEvent) -> kotlin.Unit): js.jquery.JQuery defined in js.jquery.JQuery
  public final fun click(): js.jquery.JQuery defined in js.jquery.JQuery

It works if I use the verbose way:

  jq(button).click {Element.(e: MouseClickEvent): Unit ->
  e.preventDefault()
  console.log(e)
  }

Rob


#2

Hi Rob!

It’s a bug, I’ve added an issue: http://youtrack.jetbrains.com/issue/KT-5604.
For now you can write shorter version (to emphasize it’s an extension), it will work:

jq(button).click { Element.(e) ->   e.preventDefault()   console.log(e) }


#3

Additionally You can omit parameter name(e) and use it instead, like:

  jq(button).click {   it.preventDefault()   console.log(it)   }


#4

Thanks!