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

100
Vistas
How to use a function in addEventListerner

I am learning the basics and trying to play around with JS. My question is how can I use a function written outside addEventListener. Right now I am using the following code:

ageCalculatorButton.addEventListener('click',function (event){
    var birthYear= prompt("What year you were born");
})

Now let's suppose I am trying to reuse a piece of code, how will I call the function from addEventListener. I am trying to accomplish the following:

ageCalculatorButton.addEventListener('click',ageInDays())


function ageInDays(){
    var birthYear= prompt("What year you were born");
}

Is it even possible or am I just hungup in solving an insolvable problem.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Firstly, you can reuse functions from outside.

try it like this:

ageCalculatorButton.addEventListener('click',ageInDays)


function ageInDays(){
    var birthYear= prompt("What year you were born");
}

Or even you can pass in the prompt function like this:

ageCalculatorButton.addEventListener('click',ageInDays(prompt))


function ageInDays(func){
    return function(){
        var birthYear=func("What year you were born");
    }
}
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