¿Cómo mostrar el valor de una entrada de usuario html usando JavaScript cuando se hace clic en un botón? Lo intenté, pero da "[object HTMLInputElement]" en lugar del valor ingresado por el usuario.
Aquí está el código.
<body> <p>8 + 7 = ....</p> <input type="text" name="ans" id="ans1" placeholder="Enter your answer here"> <button type="button" onclick="Game()">OK</button> <button type="button" onclick="Clear()">Cancel</button> <br><br> <h1>A Simple Math Game</h1> <p> <strong>Question 1</strong> <span id="question1"></span> <span id="answer1"></span> <span id="feedback"></span> </p> <script> document.getElementById("question1").innerHTML= "8 + 7 = ..."; function Game() { let user = document.getElementById("answer1").innerHTML= ans1; } </script>Aquí está la salida que obtuve.
8 + 7 = ....32 Aceptar (botón) Cancelar (botón)
Un simple juego de matemáticas Pregunta 1 8 + 7 = ... [objeto HTMLInputElement] // ¡este es el problema...!
<body> <p>8 + 7 = ....</p> <input type="text" name="ans" id="ans1" placeholder="Enter your answer here"> <button type="button" onclick="Game()">OK</button> <button type="button" onclick="Clear()">Cancel</button> <br><br> <h1>A Simple Math Game</h1> <p> <strong>Question 1</strong> <span id="question1"></span> <span id="answer1"></span> <span id="feedback"></span> </p> <script> document.getElementById("question1").innerHTML= "8 + 7 = ..."; function Game() { let user = document.getElementById("answer1").innerHTML= ans1; } </script> <body> <p>8 + 7 = ....</p> <input type="text" name="ans" id="ans1" placeholder="Enter your answer here"> <button type="button" onclick="Game()">OK</button> <button type="button" onclick="Clear()">Cancel</button> <br><br> <h1>A Simple Math Game</h1> <p> <strong>Question 1</strong> <span id="question1"></span> <span id="answer1"></span> <span id="feedback"></span> </p> <script> document.getElementById("question1").innerHTML= "8 + 7 = ..."; function Game() { let user = document.getElementById("answer1").innerHTML= ans1.value; } </script>