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

182
Vistas
{button}.onclick to change textContent

I'm trying make simple game "Rock Paper Scissors". At this moment I got stuck in changing value of Global Variable. Situation: (for ex.) Click => [Rock]. I have to store this into Global Variable's ("playerMove").

const rock = document.createElement('button');
/*
*/
let playerMove;
playerMove = '';

I arleady try

rock.onclick = function() {
   playerMove = 'Rock'; // Test in function..
}

but Its only work in function (Yea, we all know global variable doesn't work that way so..). I may be able to fix that problem soon but at this moment my mind can't think any better than this :|, I want help to find any good resource about this to (how) change textContent / Value of element by clicking on button.

Sorry for my bad English..

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

You can use HTMLButtonElement.addEventListener() to listen for "click" events, and then do whatever you want to, a simple demo

const btn = document.getElementById('test');
const container = document.getElementById('val');

btn.addEventListener('click', () => {
    container.textContent = 'works'
})
<div id="val">test</div>
<button id="test">click me</button>

about 4 years ago · Juan Pablo Isaza Denunciar

0

To update the playerMove variable you could use a function that set that value.

document.createElement('button');
const rock = document.querySelector('button');

let playerMove = ''
const updateMove = (move) => {
  playerMove = move;
}

rock.onclick = () => {
  updateMove('Rock');
}
about 4 years ago · Juan Pablo Isaza Denunciar

0

const rock = document.getElementById('button');
console.log(rock)
let playerMove;
playerMove = '';
rock.addEventListener('click',(e)=>{
     playerMove =e.target.innerHTML;
     console.log(playerMove);
}) 
<!DOCTYPE html> 
  <html>  
     <head>    
        <meta charset="utf-8" /> 
           <title>  Aide</title> 
           <link rel="stylesheet" href="index.css">
     </head>    
    <body>       
        <header>
        <button id="button">Rock</button>
<script type="text/javascript" src="index.js"></script>    
</body>
 </html>
strong text

This code works perfectly. it takes the text that the button contains. I think your mistake is that you didn't declare that the button you created a js is a child of body.

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