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

204
Views
how can i use a variable that is declared in a addEventList JavaScript

i am trying to find a way to be able to use the value of the playRock var outside the addEventListener, in another function. Please let me know if you have any suggestions. Bellow is my code:

Example i want to have the same value outside the Eventlistener ( line 85 same as line 81

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

0

You can use global variable outside then use it like:

let variable = '';
console.log('before eventlistener', variable);
document.querySelector('button#first').addEventListener('click', () =>{
  variable = 'ei';  
  console.log('on eventlistener', variable);
  
});
function printMe(){
  console.log('before eventlistener', variable);
}
<button id='first'>first button</button>
<button onclick='printMe();'>second button</button>

As you can press the first button will set variable, press the second will call new value of that. (if you press instead the second button without press the first you will see empty variable)

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!