Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

125
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda