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

92
Vistas
Why does js eventhandler defined in separate file run before the associated event occur

I have the following code, which works as intended

let userNameEl = document.querySelector("#username");
let startGameBtn = document.querySelector(".start-game");

startGameBtn.addEventListener("click", () => {
    if (!userNameEl.value) {
        alert("Please enter player name");
    }
}); 

But when I define the eventhandler in a separate file, the result isn't what I intended

import * as Utils from "./utils.js";

let userNameEl = document.querySelector("#username");
let startGameBtn = document.querySelector(".start-game");

startGameBtn.addEventListener("click",Utils.validatePlayerName(userNameEl));

The logic of validatePlayerName is similar to that of the anonymous function of the first addEventListener, but the result is different. It looks like Utils.validatePlayerName(userNameEl)) runs before click event is assigned.

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