currentStyle object
Represents the computed style settings for an element.
There are four objects in JavaScript that are used to retrieve and modify style settings:
- style (cross-browser)
- CSSStyleDeclaration (all browsers, except Internet Explorer before version 9)
- currentStyle (Internet Explorer and Opera)
- runtimeStyle (Internet Explorer).
In HTML, style settings can be specified for an element in three different ways:
- inline style settings (with the style attribute)
- embedded style sheets (style declarations in a style element)
- external style sheets (style sheets imported by link elements or by import style rules)
If you need the current style settings of an element, the use of the style object is sometimes not enough, because it only retrieves the inline style settings.
For that purpose, browsers support the CSSStyleDeclaration (all browsers, except Internet Explorer before version 9) and currentStyle (Internet Explorer and Opera) objects.
They retrieve the computed style for an element, regardless of whether they are defined by inline styles or embedded or external style sheets.
If no value is declared for a style property, the CSSStyleDeclaration and currentStyle objects return the default or the inherited value.
- Sometimes the retrieved style settings are in different formats than they were previously declared. For example, the CSSStyleDeclaration object retrieves RGB color values for declared predefined color names.
-
Another difference is that the shorthand style properties are partially supported by the CSSStyleDeclaration and currentStyle objects.
For example, the border is a shorthand style property, because the color, style and width of the border can be declared with it in one place.
- The border property is supported in Opera, but not supported in Internet Explorer, Firefox, Google Chrome and Safari.
- The borderColor, borderStyle and borderWidth properties are supported in Internet Explorer and Opera, but not supported in Firefox, Google Chrome and Safari.
- The borderBottomColor, borderBottomStyle, borderBottomWidth, borderLeftColor, borderLeftStyle, borderLeftWidth, ... properties are supported in Internet Explorer, Firefox, Opera, Google Chrome and Safari as well.
- Therefore, if you need the current border settings, you must use the borderBottomColor, borderBottomStyle, borderBottomWidth, ... properties for a cross-browser solution.
Note: The CSSStyleDeclaration and currentStyle objects are read-only, the style settings can not be modified by them.
There is an additional style object in Internet Explorer that helps working with style settings, it is the runtimeStyle object.
It provides one more level over the inline style settings.
It has higher precedence than the inline styles and the embedded and external style sheets.
Style properties can be accessed by the getPropertyValue, setProperty and removeProperty methods in all browsers except in Internet Explorer before version 9 and by the getAttribute, setExpression and removeAttribute methods in Internet Explorer (in all versions).
Furthermore, all four mentioned style objects support properties to directly access the value of CSS properties.
The names of these properties are often different from the names of the CSS properties.
You can find the corresponding names on the pages for the CSS properties.
When settings are modified by the runtimeStyle object, they have effect on the element (and so on the currentStyle object as well) but have no effect on the inline style settings.
The runtimeStyle object contains only the style settings specified by itself, so it can not be used to retrieve the current style settings of an element, it can only be used to retrieve the overriden style settings.
The runtimeStyle object is useful if the style settings modified at runtime need to be restored to their original value.
Syntax:
Properties that reference the object:
| object.currentStyle |
Related objects:
Related HTML objects:
a, abbr, acronym, address, applet, area, b, base, basefont, bdo, bgsound, big, blink, blockquote, body, br, button, caption, center, cite, code, col, colgroup, comment, dd, del, dfn, dir, div, dl, dt, em, embed, fieldset, font, form, frame, frameset, h1, h2, h3, h4, h5, h6, head, hr, html, i, iframe, img, input:button, input:checkbox, input:file, input:hidden, input:image, input:password, input:radio, input:range, input:reset, input:submit, input:text, ins, isindex, kbd, keygen, label, legend, li, link, listing, map, marquee, menu, meta, nobr, noframes, noscript, object, ol, optgroup, option, p, plaintext, pre, q, rt, ruby, s, samp, script, select, small, span, strike, strong, sub, sup, table, tbody, td, textarea, tfoot, th, thead, title, tr, tt, u, ul, var, wbr, xml, xmp
|
The base interface, through which you can add new functionalities to the currentStyle object, is the CSSStyleDeclaration interface.
Possible members:
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Example HTML code 1:
This example uses the style object to retrieve the background color of an element when it is specified by an inline style:
|
||||
<head> <script type="text/javascript"> function GetBGColor (button) { alert (button.style.backgroundColor); } </script> </head> <body> <button onclick="GetBGColor (this)" style="background-color:red">Get my background color!</button> </body> |
||||
|
||||
Did you find this example helpful?
|
Example HTML code 2:
This example uses the CSSStyleDeclaration and currentStyle objects to retrieve the background color of an element when it is specified by an inline style:
|
||||
<head> <script type="text/javascript"> function GetBGColor (button) { if (window.getComputedStyle) { var compStyle = window.getComputedStyle (button, ""); } else { var compStyle = button.currentStyle; } alert (compStyle.backgroundColor); } </script> </head> <body> <button onclick="GetBGColor (this)" style="background-color:red">Get my background color!</button> </body> |
||||
|
||||
Did you find this example helpful?
|
Example HTML code 3:
This example uses the CSSStyleDeclaration and currentStyle objects to retrieve the background color of an element when it is specified by an embedded style:
|
||||
<head> <style> .myButton { background-color: red; } </style> <script type="text/javascript"> function GetBGColor (button) { if (window.getComputedStyle) { var compStyle = window.getComputedStyle (button, ""); } else { var compStyle = button.currentStyle; } alert (compStyle.backgroundColor); } </script> </head> <body> <button onclick="GetBGColor (this)" class="myButton">Get my background color!</button> </body> |
||||
|
||||
Did you find this example helpful?
|
Related pages:
External links:
User Contributed Comments