Kotlin JS - Accessing HTML DOM properties -
what canonical way access html dom properties in kotlin. don't see of dom properties offsetheight & offsetwidth exposed in element
var e : element? = document.getelementbyid("text") e.offsetheight //error
just cast e htmlelement, gets properties expect.
(e htmlelement).offsetheight it not feature of kotlin, found in normal js documentation: https://developer.mozilla.org/en-us/docs/web/api/htmlelement/offsetheight
also
you right ask 'canonical' way things, since kotlin quite different javascript internally. here how code snippet:
val e = document.getelementbyid("text")!! htmlelement e.offsetheight use
valinstead ofvarin code. states fixed reference , allows code optimizations. http://kotlinlang.org/docs/reference/properties.html#properties-and-fieldsdon't use nullable types
element?if don't need it. in case may pretty sure dom structure,getelementbyid("text")must return element, not null. put null-assertion!!there easy mind. in case js works unknown html, handle situation better:val e = document.getelementbyid("text") as? htmlelement ?: throw runtimeexception("the dom has no 'text' id")
Comments
Post a Comment