Why does style.textDecoration contain an rgb value?
style = window.getComputedStyle(document.getElementById('gettextDecorationfrom'));
weight = style.textDecoration;
document.getElementById("results").innerText = style.textDecoration;
.myclass {
text-decoration:initial;
}
<div id="gettextDecorationfrom" class="myclass">
Test
</div>
<hr>
Result: <div id="results"></div>
I was expecting "initial" to be the result from this but was not expecting an rgb value to show up.
If underline is set the rgb value is also still there but just with 'underline' added, I don't understand why rgb is there in the first place?
style = window.getComputedStyle(document.getElementById('gettextDecorationfrom'));
weight = style.textDecoration;
document.getElementById("results").innerText = style.textDecoration;
.myclass {
text-decoration:underline;
}
<div id="gettextDecorationfrom" class="myclass">
Test
</div>
<hr>
Result: <div id="results"></div>
Can I remove the rgb value from text-decoration no matter what the value is?
CodePudding user response:
Per the docs, text-decoration is a shorthand property. See: https://developer.mozilla.org/en-US/docs/Web/CSS/text-decoration
That means it contains multiple properties. Specifically, it contains text-decoration-line
, text-decoration-color
, text-decoration-style
, and text-decoration-thickness
. That's why you see the RGB value, because it includes color in the text-decoration
property.
If you just want the text-decoration-line property, then you can use textDecorationLine
, but that still will never give you initial
as you mentioned in your first example.
That's because getComputedStyle
does not return what is in the CSS; it returns the style that actually ends up getting applied the element. That's why it's called computed style. initial
is not a computed style, it's a value that instructs the browser to compute the style by using whatever the initial style was, which for text-decoration-line is none, as you can see here: https://developer.mozilla.org/en-US/docs/Web/CSS/text-decoration-line#formal_definition
If you want to get that initial
, you will have to actually inspect the stylesheet itself. See: https://developer.mozilla.org/en-US/docs/Web/API/CSSStyleSheet
CodePudding user response:
text-decoration
has a default color, usually your browser's (black)
You can trim of the rgb part of the string with .replace
and a bit of regex.
This will cut out the rgb part regardless of the rgb(x.x.x) values, as you wanted.
BUT! You wont be able to get initial
as a result, because initial
replaces to the default none
value at text-decoration
style = window.getComputedStyle(document.getElementById('gettextDecorationfrom'));
weight = style.textDecoration;
text = weight.replace(/ ?rgb\(([^;]*)\)/gm, '');
document.getElementById("results").innerText = text;
.myclass {
text-decoration:underline;
}
<div id="gettextDecorationfrom" class="myclass">
Test
</div>
<hr>
Result: <div id="results"></div>
CodePudding user response:
Using a combination of both answers I was able to get what i needed.
The answer by Instance Hunter had the JavaScript textDecorationLine
i was missing which allowed me to use FHJTDSDG's answer without the regex.
Working example..
updateresult()
function toggleunderline() {
if (weight === "none") {
document.getElementById("gettextDecorationfrom").style.textDecorationLine = "underline"
updateresult()
} else {
document.getElementById("gettextDecorationfrom").style.textDecorationLine = "initial"
updateresult()
}
}
function updateresult(){
style = window.getComputedStyle(document.getElementById('gettextDecorationfrom'));
weight = style.textDecorationLine;
document.getElementById("results").innerText = weight;
}
.myclass {
text-decoration:initial;
}
<button onclick="toggleunderline()">
Toggle underline
</button>
<div id="gettextDecorationfrom" class="myclass">
Test
</div>
<hr>
Result: <div id="results"></div>