0

I want to define a function inside Kotlin/transpiled JavaScript, that I can pass "in it's raw and global form" to some inputs inside my html. With raw form, I mean not as [project name].doClick().

<input type="button" value="Click me!" onclick="doClick();>
2
  • Ah, I see. You know about fully qualified reference. Is there any reason you don't want to pass it? Commented Nov 8, 2017 at 17:02
  • @AlexeyAndreev Hmm... I don't know, what fully qualified references are :P. The only reason, I don't want to pass it with namespace prefix is for sake of look. It looks more "simple javascript" and nicer to me. HelI can't even descripe, why I want to avoid that. I guess there's no proper reason and I'm gonna just use a namespace. EDIT: I'm not a Web developer at all Commented Nov 25, 2017 at 20:41

2 Answers 2

1

I was struggling with similar problem, although specifying a module was not enough in my case. So referring to the initial question Define global function in Kotlin I would suggest a workaround:

import kotlin.browser.window

fun main() {
    window.asDynamic()["doClick"] = ::doClick
}

fun doClick() {
    println("Hello, world")
}

In this code a kotlin function is explicitly assigned to a variable in global scope. Now you can simply call your function using doClick()

Sign up to request clarification or add additional context in comments.

Comments

0

Consider you have module named sample, with either plain or UMD module type. It's content is following:

package foo

fun doClick() {
    println("Hello, world")
}

Then you can assign it to click handler as follows:

<input type="button" value="Click me!" onclick="sample.foo.doClick();>

See documentation for details.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.