Skip to content

Commit 929deef

Browse files
skyclouds2001github-actions[bot]wbambergdipikabh
authored
update {HTMLElement,SVGElement,MathMLElement}.style live example (mdn#31631)
* update * revert * update example * fix examples * Apply suggestions from code review Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * fix * Apply suggestions from code review Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * Apply suggestions from code review * fix * Apply suggestions from code review Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * update --------- Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: wbamberg <[email protected]> Co-authored-by: Dipika Bhattacharya <[email protected]>
1 parent 034ea75 commit 929deef

File tree

3 files changed

+22
-7
lines changed

3 files changed

+22
-7
lines changed

files/en-us/web/api/htmlelement/style/index.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,17 +52,22 @@ const element = document.getElementById("elt");
5252
const out = document.getElementById("out");
5353
const elementStyle = element.style;
5454

55-
// We loop through all styles (for…of doesn't work with CSSStyleDeclaration)
55+
// We loop through all the element's styles using `for...in`
5656
for (const prop in elementStyle) {
57-
if (Object.hasOwn(elementStyle, prop)) {
57+
// We check if the property belongs to the CSSStyleDeclaration instance
58+
// We also ensure that the property is a numeric index (indicating an inline style)
59+
if (
60+
Object.hasOwn(elementStyle, prop) &&
61+
!Number.isNaN(Number.parseInt(prop))
62+
) {
5863
out.textContent += `${
5964
elementStyle[prop]
6065
} = '${elementStyle.getPropertyValue(elementStyle[prop])}'\n`;
6166
}
6267
}
6368
```
6469

65-
{{EmbedLiveSample("Getting_style_information", "100", "115")}}
70+
{{EmbedLiveSample("Getting_style_information", "100", "130")}}
6671

6772
Note `font-weight` is not listed as a value for `elementStyle` as it is not defined within the `style` attribute of the element itself. Rather, it is inherited from the definition on its parent. Also note that the shorthand {{cssxref("border-top")}} property, defined in the `style` attribute, is not listed directly. Rather, it is replaced by the three corresponding longhand properties ({{cssxref("border-top-color")}}, {{cssxref("border-top-style")}}, and {{cssxref("border-top-width")}}).
6873

files/en-us/web/api/mathmlelement/style/index.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,14 @@ const element = document.querySelector(".parameter");
5555
const out = document.getElementById("out");
5656
const elementStyle = element.style;
5757

58-
// We loop through all styles (for…of doesn't work with CSStyleDeclaration)
58+
// We loop through all the element's styles using `for...in`
5959
for (const prop in elementStyle) {
60-
if (Object.hasOwn(elementStyle, prop)) {
60+
// We check if the property belongs to the CSSStyleDeclaration instance
61+
// We also ensure that the property is a numeric index (indicating an inline style)
62+
if (
63+
Object.hasOwn(elementStyle, prop) &&
64+
!Number.isNaN(Number.parseInt(prop))
65+
) {
6166
out.textContent += `${
6267
elementStyle[prop]
6368
} = '${elementStyle.getPropertyValue(elementStyle[prop])}'\n`;

files/en-us/web/api/svgelement/style/index.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,14 @@ const element = document.querySelector("circle");
5959
const out = document.getElementById("out");
6060
const elementStyle = element.style;
6161

62-
// We loop through all styles (for…of doesn't work with CSStyleDeclaration)
62+
// We loop through all the element's styles using `for...in`
6363
for (const prop in elementStyle) {
64-
if (Object.hasOwn(elementStyle, prop)) {
64+
// We check if the property belongs to the CSSStyleDeclaration instance
65+
// We also ensure that the property is a numeric index (indicating an inline style)
66+
if (
67+
Object.hasOwn(elementStyle, prop) &&
68+
!Number.isNaN(Number.parseInt(prop))
69+
) {
6570
out.textContent += `${
6671
elementStyle[prop]
6772
} = '${elementStyle.getPropertyValue(elementStyle[prop])}'\n`;

0 commit comments

Comments
 (0)