Avoiding Casting

From PowerUI
Revision as of 22:05, 9 February 2017 by 151.229.186.156 (talk) (Created page with "PowerUI 2 has a standardised DOM. That means that methods like '''getElementById''' return an '''Element''' rather than the more useful '''HtmlElement''', which results in a l...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

PowerUI 2 has a standardised DOM. That means that methods like getElementById return an Element rather than the more useful HtmlElement, which results in a lot of casting that previously wasn't required.

Why does it happen at all?

HTML5 can have content like SVG and MathML embedded within it; an SvgElement is not a HtmlElement. However, the vast majority of the time, you'll be interacting with a HtmlElement (and on the web, Javascript hides this from you).

Convenience functions

If you find an instance where a convenience function would be useful, please let us know!

(these ones have the bonus of being a little shorter too - just omit the word Element from the standard Web API)

Class Standard name Convenience name What it does
Document getElementById getById Element to HtmlElement
Document getElementByTagName getByTagName Element to HtmlElement
Document getElementByAttribute getByAttribute Element to HtmlElement
Event target htmlTarget Element to HtmlElement