Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

199
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!