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

180
Views
{button}.onclick para cambiar textContent

Estoy tratando de hacer un juego simple " Piedra, papel o tijera" . En este momento me quedé atascado cambiando el valor de la variable global. Situación: (por ej.) Haga clic en => [Rock]. Tengo que almacenar esto en Global Variable's (" playerMove ").

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

ya lo intento

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

pero solo funciona en función (sí, todos sabemos que la variable global no funciona de esa manera, así que ...). Es posible que pueda solucionar ese problema pronto, pero en este momento mi mente no puede pensar en nada mejor que esto:|, quiero ayuda para encontrar un buen recurso sobre esto para (cómo) cambiar textContent/Valor del elemento haciendo clic en el botón .

Perdón por mi mal ingles..

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Puede usar HTMLButtonElement.addEventListener() para escuchar eventos de "clic" y luego hacer lo que quiera, una demostración simple

 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

Para actualizar la variable playerMove, puede usar una función que establezca ese valor.

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

Este código funciona perfectamente. toma el texto que contiene el botón. Creo que su error es que no declaró que el botón que creó un js es un elemento secundario del cuerpo.

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!