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

186
Visualizações
Find css class on div and get values

I'm trying to find a class on a div (where the value will always end in 0bg), given a partial string, and then get its background style value.

For example:

.xx-color-100bg{
    background-color: #323334;
}
<div class="heading xx-color-100bg"></div>
document.querySelectorAll(".heading").classList.contains("0bg").style.background

The solution above errors with Cannot read properties of undefined (reading 'contains')

How can I find the necessary class and grab it's background-color value?

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

0

querySelectorAll() returns a NodeList, so you can't use classList. You need to loop over the DOMElements in the list or just assume the first one is what you need:

document.querySelectorAll(".heading").forEach(e => {
    // do something with e.classList
});

// OR

document.querySelectorAll(".heading")[0].classList // the first element
about 4 years ago · Santiago Gelvez Relatório

0

Figured out the solution right after posting the question:

window.getComputedStyle(document.querySelector(".heading[class*='0bg'")).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