Estoy tratando de obtener el valor de un tipo de entrada de número, pero cuando intento mostrar el valor, devuelve un valor nulo (ps: lo siento, todavía soy un estudiante de secundaria) HTML:
<label for="nummatch"><h2>Number of Match/es:</h2></label> <input type="number" onkeyup="stop(this)" id="nummatch" name="number of matches" value="1" min="1" max="5"> <br> <a href="../html/Game Page.html#vsComputer"><button onclick="number()"><h1>VS Computer</h1></button></a>JavaScript
function number(){ var times = document.getElementById("nummatch").value; return timesNum; } var time = number(); console.log(time)el archivo console.log devuelve un error "Error de tipo no detectado: no se pueden leer las propiedades de nulo (leyendo 'valor')"
Como puedo ver, está devolviendo un nombre de variable diferente dentro de la función 'número'.
Aquí hay un código JavaScript correcto con código HTML completo:
Finalmente, espero haber sido claro y útil, ¡Gracias!.
<label for="nummatch"><h2>Number of Match/es:</h2></label> <input type="number" onkeyup="stop(this)" id="nummatch" name="number of matches" value="1" min="1" max="5"> <br> <a href="../html/Game Page.html#vsComputer"><button onclick="number()"><h1>VS Computer</h1></button></a> <script> function number() { var times = document.getElementById("nummatch").value; return times; } var time = number(); console.log(time) </script>Algo falta. Agrega un evento onkeyup que llama a la función stop(). Agrego esta función a su código y funciona bien.
function stop() { var time = number(); console.log(time) } function number(){ var times = document.getElementById("nummatch").value; return times; } <label for="nummatch"><h2>Number of Match/es:</h2></label> <input type="number" onkeyup="stop(this)" id="nummatch" name="number of matches" value="1" min="1" max="5"> <br> <a href="../html/Game Page.html#vsComputer"><button onclick="number()"><h1>VS