Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

206
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda