Calling Kotlin from HTML(Javascript), TypeError problem


I get browser error: “TypeError, …Is not a function”, when i try to pass (this) as argument to the Kotlin function from HTML input. Calling a pure javascript function works just fine. I cannot find anything wrong with the compiled Javascript either.

in HTML:
input type=“radio” onclick=“Mymodule.test(this)”

What am i doing wrong? If i remove the argument (this), it works.

Hi Latesoft!

Could you provide more information about Mymodule.test function and where exactly error occurs?

It would be awesome if you could make a small, self-contained code snippet!

Attached is blank Idea sample project with the problem. Debugged with Chrome v. 77.0.3865.120 (64-bit). (324.3 KB)

ps. Realized that “test()” is actually built in method in kotlin, threw me off for a while when trying the sample.

Ok i accidentally stumbled on the solution at stackoverflow

You need to name the method with @JsName to start accepting arguments to the function. This must be a bug or unwanted feature?

BTW, kotlin html does not resolve the function, unless you use pure javascript. This too must be some sort of disconnect within the compiler.

Using @JsName is the right solution indeed. Having to use @JsName when calling Kotlin function from JS is intended feature. Its not very pretty IMO, but it is there because JavaScript doesn’t support function overloading. More about it here: Mandatory method mangling when compiling to JS

The documentation about Javascript - Kotlin interop needs to be updated as i did not overload anything here.

Could you take a look how the html editor works here. It says “unresolved function” whilst flickering to the suggested “red dot” item, containing the exact unresolved function. The mouse hotspot is at the dot separating the method and package.