Developers
How to target a single element without getElementById?
The UWA environment disables the document and window object for security and portability purposes, but retains some of their methods and events in its widget object (see ” using JavaScript and Ajax”).
getElementById, while very useful in itself, has not been kept in widget because there can be multiple instances of a given widget, and thus multiple elements with the same id.
However, widget had both getElementsByTagName and getElementsByClassName methods, which can both be used to achieve the same result as getElementById would, if they are applied to widget.body, which references the widget's XHTML tags.
widget.body.getElementsByTagName and widget.body.getElementsByClassName each return an Array of elements, which is not directly usable unless you use a loop. You can use these methods as you would use getElementById by having only one element feature a given class, for instance, and targetting it as the first item from the getElementsByClassName array:
var statusZone = widget.body.getElementsByClassName("statuszone")[0];
It's even cleaner when the targetted element is the only one using a tag name - because you don't have to assign a dummy class to an element just in order to target it:
var textarea = widget.body.getElementsByTagName("textarea")[0];
The getElemtentsBy* method are not the only ways. If you dynamically generate your XHTML tags using JavaScript, you can directly obtain a reference for the element using the createElement method - reference which can the be used to target said new element:
var myElement = widget.body.createElement;
- Send to a friend
- Add to favorites
- Last modified: 2008/04/24 23:27

