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

282
Visualizações
jQuery or vanilla JS - return r, g & b values for background color of element?

I'm not sure why this one is haunting me. I have a grid of event cards. Each card has a button with the same background color. How do I return the rbg value of that button class, in this exampleevent-button?

<div class="event-grid">
  <div class="event-card">
    <button class="event-button">Buy Tickets</button> // return r, g & b values for bg color
  </div>
  <div class="event-card">
    <button class="event-button">Buy Tickets</button> 
  </div>
  <div class="event-card">
    <button class="event-button">Buy Tickets</button> 
  </div>
  <div class="event-card">
    <button class="event-button">Buy Tickets</button> 
  </div>
</div>

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

If you want to get the r, g, b and a value of the css class definition, you can use getComputedStyle Reference

Please find the working fiddle.

document.querySelectorAll('button.event-button').forEach((button) => {
  button.onclick = function(e) {
    const styles = window.getComputedStyle(e.target);
    const background = styles.getPropertyValue("background-color");
    const [red, green, blue, alpha] = background.replace(/^(rgb|rgba)\(/,'').replace(/\)$/,'').replace(/\s/g,'').split(',');
    console.log(red, green, blue);
  }
});
.event-button {
  background: rgba(255,0,0,0.3);
}
<div class="event-grid">
  <div class="event-card">
    <button class="event-button">Buy Tickets</button> // return r, g & b values for bg color
  </div>
  <div class="event-card">
    <button class="event-button">Buy Tickets</button>
  </div>
  <div class="event-card">
    <button class="event-button">Buy Tickets</button>
  </div>
  <div class="event-card">
    <button class="event-button">Buy Tickets</button>
  </div>
</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