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

136
Visualizações
console.log of backgroundColor shows "" instead of the color value

I have an button element with id "btn". I want to console.log the background color property it shows blank instead. What is the problem? Thank you.

let btnColor = document.getElementById("btn").style.backgroundColor;
console.log(btnColor)
#btn{
  display: block;
  margin: auto;
  width: 100px;
  height: 100px;
  border-radius: 3px;
  background-color: #74992e;
}
<button id="btn">Click on me</button>

about 4 years ago · Santiago Gelvez
1 Respostas
Responde à pergunta

0

Use this:

let btnColor = window.getComputedStyle(document.getElementById(“btn”)).backgroundColor
console.log(btnColor)

it’s working fine for me

explain: sometimes window.getComputedStyle will get the style object of an elem so that if you call .backgroundColor then it’s working fine

summary two functions :)

  • window.getComputedStyle(element).backgroundColor

  • element.style.backgroundColor

about 4 years ago · Santiago Gelvez 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