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

耗尽温柔 提交于 2020-01-04 11:10:54

问题


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:


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
}


来源:https://stackoverflow.com/questions/30819811/how-to-access-this-element-from-event-handler-passed-into-scalatags

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!