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

118
Views
¿Mi botón no responde a mi detector de eventos?

A continuación se muestra mi código. Estoy tratando de generar un color hexadecimal aleatorio que reemplazará el color de fondo cuando se haga clic en mi botón, pero no puedo hacer que funcione.

índice.html

 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Color Changer</title> </head> <body> <h1> <button id="newColor">Click Me</button> </h1> </body> </html>

scripts.js

 const btn = document.getElementById("newColor"); btn.addEventListener("click", function onClick(event) { let randColor = "#" + Math.floor(Math.random()*16777215).toString(16); document.body.style.backgroundColor = randColor; });
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

  1. incluya su archivo js en el archivo html:
 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Color Changer</title> <script src="scripts.js"></script> </head> <body> <h1> <button id="newColor">Click Me</button> </h1> // or include here //<script src="scripts.js"></script> </body> </html>
  1. puede usar la carga de eventos de js igual que:
 window.onload = function () { const btn = document.getElementById("newColor"); btn.addEventListener("click", function onClick(event) { let randColor = "#" + Math.floor(Math.random()*16777215).toString(16); document.body.style.backgroundColor = randColor; }); };
  1. puede usar jquery con $( documento ).ready()
about 4 years ago · Juan Pablo Isaza Report

0

el código está funcionando, ¿su código js está después de la etiqueta HTML?

 const btn = document.getElementById("newColor"); btn.addEventListener("click", function onClick(event) { let randColor = "#" + Math.floor(Math.random()*16777215).toString(16); document.body.style.backgroundColor = randColor; });
 <h1> <button id="newColor">Click Me</button> </h1>

about 4 years ago · Juan Pablo Isaza 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!