Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

129
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda