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

106
Views
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 answers
Answer question

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 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!