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

130
Vistas
can we declare a function inside of eventlistner and call it from outside?

Here I have declared a function "driverDetailsValidation" within the event listener. I want to call it from outside the event listener. This is the code which I have used but it doesn't work

driverType.onchange = function(e) {
        var isWithoutDriver = (driverType.value == "without driver");
        if(!isWithoutDriver){
            driverDetailsValidation();
        }
    }

    const driver_fields = document.getElementsByClassName("driver_fields");
    
    form.addEventListener("submit", (event) => {
        validity = true;
    
        function driverDetailsValidation() {
            for (let i = 0; i < driver_fields.length; i++) {
                if (driver_fields[i].value == "") {
                    driver_fields[i].style.border = "2px solid rgb(228, 29, 22)";
                    driver_fields[i].style.backgroundColor = "rgba(238, 156, 156, 0.788)";
                    //fields[i].placeholder = "This Field is Compulsory!";
                    validity = false;
                }
            }
        }
    }
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You could declare a variable outside the event listener and assign the function to it inside the listener. Then, you can call the function from the variable like this:

var fct;

document.querySelector("div").addEventListener("click", function() {
  fct = function() {
    alert("Working");
  }
});

setInterval(function() {
  if (fct != null) {
    fct();
    fct = null;
  }
}, 0);
<div>
Click me
</div>

But it doesn't really make sense, I think.

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