Kotlin js: addEventListener does not work

when i call following code, the on click even does not work. it shows the created button, but does not print the line “button clicked” when i click it, nor does the html show any onclick event inside the html-button:

val button1 = document.createElement("button")
button1.addEventListener("onclick",{ println("button clicked") });

what am i doing wrong?

The correct usage of this function would be:

button1.addEventListener("click") { println("button clicked") }

your code was not valid. i guess you meant this one?

button1.addEventListener("click", { println("button clicked") })

thanks that works. the only problem is that it only works once after the first click but does not write the same line again when i click the button a second time.

i just realized that the second click only does not work if the print value stays the same. with this code i get multiple output lines if i change the value of the input after each click.

button1.addEventListener("click", {
    val input= document.getElementById("input") as HTMLInputElement
    println("button clicked: " + input.value)

still i would like to know why in the code above the click only works one time.

modern browsers to log cleanup, on right side there will be counter. something like “last message repeated N times”

1 Like

i am using the intellij javascript debugger, and it does not show any information how often the same line was printed, but good to know that its just a display problem.