Could you tell me why we need to use the getPropertyValue method if we can use only the getComputedStyle one?
For example, this will work, as far as I understand:
var s = getComputedStyle(element, null).opacity;
Which is equivalent to the following:
var s = getComputedStyle(element, null).getPropertyValue('opacity');
Can we use getComputedStyle without getPropertyValue?
According to the old DOM L2 Style, getPropertyValue was not required:
The
CSS2Propertiesinterface represents a convenience mechanism for retrieving and setting properties within aCSSStyleDeclaration. The attributes of this interface correspond to all the properties specified in CSS2. Getting an attribute of this interface is equivalent to calling thegetPropertyValuemethod of the CSSStyleDeclaration interface. Setting an attribute of this interface is equivalent to calling thesetPropertymethod of theCSSStyleDeclarationinterface.
However, implementations were not required to support it, so using getPropertyValue was safer.
A conformant implementation of the CSS module is not required to implement the
CSS2Propertiesinterface.
But according to the newer CSSOM, using camel-case without getPropertyValue must work:
For each CSS property property that is a supported CSS property, the following partial interface applies where camel-cased attribute is obtained by running the CSS property to IDL attribute algorithm for property.
partial interface CSSStyleDeclaration { attribute DOMString _camel-cased attribute; };The
camel-cased attributeattribute, on getting, must return the result of invokinggetPropertyValue()with the argument being the result of running the IDL attribute to CSS property algorithm for camel-cased attribute.Setting the
camel-cased attributeattribute must invokesetProperty()with the first argument being the result of running the IDL attribute to CSS property algorithm for camel-cased attribute, as second argument the given value, and no third argument. Any exceptions thrown must be re-thrown.
Therefore, getPropertyValue is no longer necessary to retrieve CSS values.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With