Setting style property values

You use the UIObject.setStyle() method to set a style property on a component instance, the global style declaration, a custom style declaration, or a class style declaration. The following code sets the color style of a radio button instance to red:

myRadioButton.setStyle("color", "red");

The following code sets the color style of the custom style declaration CheckBox:

_global.styles.CheckBox.setStyle("color", "white");

The UIObject.setStyle() method knows if a style is inheriting and notifies children of that instance if their style changes. It also notifies the component instance that it must redraw itself to reflect the new style. Therefore, you should use setStyle() to set or change styles. However, as an optimization when creating style declarations, you can directly set the properties on an object. For more information, see Setting global styles, Setting styles for specific components, and Setting styles for a component class.

You use the UIObject.getStyle() method to retrieve a style from a component instance, the global style declaration, a custom style declaration, or a class style declaration. The following code gets the value of the color property and assigns it to the variable o:

var o = myRadioButton.getStyle("color");

The following code gets the value of a style property defined on the _global style declaration:

var r = _global.style.getValue("marginRight");

If the style isn't defined, getStyle() may return the value undefined. However, getStyle() understands how style properties inherit. So, even though styles are properties, you should use UIObject.getStyle() to access them so you don't need to know whether the style is inheriting.

For more information, see UIObject.getStyle() and UIObject.setStyle().