The get()
method of the StylePropertyMapReadOnly
interface returns a CSSStyleValue
object for the first value of the specified property.
The get()
method of the StylePropertyMapReadOnly
interface returns a CSSStyleValue
object for the first value of the specified property.
js
get(property)
property
The name of the property to retrieve the value of.
A CSSStyleValue
object.
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:
html
<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:
css
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.
js
// get the element const myElement = document.querySelector("a"); // Retrieve all computed styles with computedStyleMap() const styleMap = myElement.computedStyleMap(); // get the <dl> we'll be populating const stylesList = document.querySelector("#results"); // array of properties we're interested in const ofInterest = ["font-weight", "border-left-color", "color", "--color"]; // iterate over our properties of interest for (const property of ofInterest) { // properties const cssProperty = document.createElement("dt"); cssProperty.innerText = property; stylesList.appendChild(cssProperty); // values const cssValue = document.createElement("dd"); // use get() to find the value cssValue.innerText = styleMap.get(property); stylesList.appendChild(cssValue); }
Specification |
---|
CSS Typed OM Level 1 # dom-stylepropertymapreadonly-get |
Desktop | Mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | WebView Android | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | |
get |
66 | 79 | No | No | 53 | 16.4 | 66 | 66 | No | 47 | 16.4 | 9.0 |
© 2005–2023 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/StylePropertyMapReadOnly/get