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

191
Views
Cambie el color del botón dependiendo de la condición con vanilla js/css

Quiero cambiar el color del botón dependiendo de alguna condición.

En pseudocódigo se ve así:

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

Pero debe verificarse y aplicarse antes de cargar la página.

¡Gracias!

about 4 years ago · Santiago Gelvez
1 answers
Answer question

0

Como desea que aplique el estilo antes de que se pinte el DOM, puede usar el evento DOMContentLoaded , ya que este evento se activa tan pronto como los nodos DOM hayan terminado de cargarse, pero antes de que se hayan cargado todos los recursos, estilos, etc.

Algo como esto debería hacerlo:

 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!