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

185
Views
{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 answers
Answer question

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 Report

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 Report

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