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

203
Vistas
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 Respuestas
Responde la pregunta

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