Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

120
Views
javascript style.textDecoration contains an unexpected rgb value?

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>
I was expecting "underline" to be the result from this but was not expecting an rgb value as well to show up.

Can I remove the rgb value from text-decoration no matter what the value is?

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

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>

about 4 years ago · Juan Pablo Isaza Report

0

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

about 4 years ago · Juan Pablo Isaza Report

0

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>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!