Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

98
Views
Cómo usar una función en addEventListerner

Estoy aprendiendo lo básico y tratando de jugar con JS. Mi pregunta es cómo puedo usar una función escrita fuera de addEventListener. Ahora mismo estoy usando el siguiente código:

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

Ahora supongamos que estoy tratando de reutilizar un fragmento de código, ¿cómo llamaré a la función desde addEventListener? Estoy tratando de lograr lo siguiente:

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

¿Es posible o simplemente estoy obsesionado con resolver un problema insoluble?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

En primer lugar, puede reutilizar funciones desde el exterior.

inténtalo así:

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

O incluso puede pasar la función de prompt de esta manera:

 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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!