How to access 'this' element from event handler passed into scalatags?

400 views Asked by At

I'm trying to access the text of the current (this) element from within an event handler created with scalatags. Here is what I tried:

val onChange = {(e: HTMLElement) =>
  number() = e.textContent.toInt
}: js.ThisFunction

input(`type`:="number", onchange := onChange).render

When I debug the above code, nothing is being passed into the onChange function. Specifically, if I put this into the function body: js.Dynamic.global.alert(JSON.stringify(e)), it prints {}. Also, I get an error that e.textContent is null. How do I get it to pass in the javascript this element?

1

There are 1 answers

1
Julie On

I got some clarification on scala.js gitter, and it turns out you can access the element from within a closure like so:

val inputElem = input(`type`:="number").render
inputElem.onchange = {(e: Event) =>
  number() = inputElem.value.toInt
}