I'm trying to get the value from an number input type but when i try to show the value it returns a null (ps: sorry I'm still a highschool student) 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)
the console.log returns a error "Uncaught TypeError: Cannot read properties of null (reading 'value')"
As I can see, you're returning a different variable name inside the 'number' function.
Here is a correct JavaScript code with full HTML Code:
Finally, I hope that's clear and helpful, Thanks!.
<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>
Something is missing. You add a onkeyup event which calls the function stop(). I add this function to your code and it works well.
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