Entry
Why do I get errors when using CSS property names in script
Apr 27th, 2000 20:43
Rey Nuņez, jsWalter,
There is a special consideration when accessing CSS attribute names in
script.
Many CSS properties have a dash in their name (such as background-
color). The dash, however, is not a valid character for identifiers in
most scripting languages. To get around this little problem, continue
to use the dashed name when specifying the CSS attribute in HTML or in
a style sheet, but when accessing the attribute as a script property,
remove the dash, and capitalize the next letter. For example:
background-color becomes backgroundColor
border-left becomes borderLeft
margin-bottom becomes marginBottom
This rule applies to all CSS attribute names, meaning all CSS attribute
names that may be specified in an HTML page or in style sheets
correspond as scriptable properties of the style object.
Note that while any CSS property can be set at any time, the current
settings retrieved on any element's style object will only reflect its
inline styles, and not any inherited styles that are defined in an
embedded or linked style sheet declared via a STYLE or LINK element.
For instance, when a style is defined both globally and inline, such as
font-family, referencing style.fontFamily will be valid only on
elements that had the font-family property set with an inline style or
was previously set with script.