The get()
method of the StylePropertyMapReadOnly
interface returns a CSSStyleValue
object for the first value of the specified property.
Let's get just a few properties and values. Let's start by creating a link inside a paragraph in our HTML, and adding a definition list which we will populate with JavaScript:
<p>
<a href="https://example.com">Link</a>
</p>
<dl id="results"></dl>
We add a bit of CSS, including a custom property and an inheritable property:
p {
font-weight: bold;
}
a {
--color: red;
color: var(--color);
}
We use the Element's computedStyleMap()
to return a StylePropertyMapReadOnly object. We create an array of properties of interest and use the StylePropertyMapReadOnly's get()
method to get only those values.
const myElement = document.querySelector('a');
const styleMap = myElement.computedStyleMap();
const stylesList = document.querySelector('#results');
const ofInterest = ['font-weight', 'border-left-color', 'color', '--color'];
for ( let i = 0; i < ofInterest.length; i++ ) {
const cssProperty = document.createElement('dt');
cssProperty.innerText = ofInterest[i];
stylesList.appendChild(cssProperty);
const cssValue = document.createElement('dd');
cssValue.innerText = styleMap.get(ofInterest[i]);
stylesList.appendChild(cssValue);
}