-2

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

1 Answers1

-1

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)

Simone Rossaini
  • 8,115
  • 1
  • 13
  • 34