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

192
Visualizações
Change the colour of the button dependently on condition with vanilla js/css

I want to change the color of the button dependently on some condition.

In pseudo code it looks like this:

if(condition){
    myelememt.background = "something"
}

But it should be checked and applied before the page is loaded.

Thank you!

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

0

Since you want it to apply the style before DOM paints, you can use DOMContentLoaded event, since this event fires as soon as the DOM nodes have finished loading, but before all assets, styles, etc... have been loaded.

Something like this should make it:

const condition = true;
document.addEventListener('DOMContentLoaded', (event) => {
  const button = document.querySelector('button');
  if (condition) button.style.background = 'red';
});
<html>
  <head>
    <meta charset="UTF-8" />
    <script src="script.js"></script>
    <link rel="stylesheet" type="text/css" href="styles.css" />
  </head>
  <body>
    <button>CLICK</button>
  </body>
</html>

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