Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

121
Vistas
My button is not responding to my event listener?

Below is my code. I am trying to generate a random hex color that will replace the background color when my button is clicked but I can't get it to work.

index.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 Respuestas
Responde la pregunta

0

  1. include your js file to html file :
    <!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. can use event onload of js same as :
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. can use jquery with $( document ).ready()
about 4 years ago · Juan Pablo Isaza Denunciar

0

the code is working is your js code is after the HTML tag?

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda