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 
  1. use val instead of var in code. states fixed reference , allows code optimizations. http://kotlinlang.org/docs/reference/properties.html#properties-and-fields

  2. don'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

Popular posts from this blog

PHP DOM loadHTML() method unusual warning -

python - How to create jsonb index using GIN on SQLAlchemy? -

c# - TransactionScope not rolling back although no complete() is called -