I’m learning JavaScript and I made some codes for training and my code isn’t working and I don’t know why
This is the error:
Error handling response: TypeError: Cannot set properties of null (setting ‘valueAsNumber’)
Here is my code which I checked and I did not find any problem but it says that I have three errors
<!DOCTYPE html>
<html lang="pt-BR">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Detran</title>
</head>
<body>
<h1>Sitema de Multas</h1>
Velocidade do carro <input type="number" name="txtvel" id="txtvel"> Km/h
<input type="button" value="Verificar">
<div id='res'>
</div>
<script>
function calcular() {
var txtv = window.document.querySelector('input#txtvel')
var res = window.document.querySelector('div#res')
var vel = Number(txtv.value)
res.innerHTML = `Sua velocidade atual é de ${vel}Km/h`
}
</body>
</html>
2
Answers
You have not closed script tag
also add onclick attribute to the button
The error
Means that somewhere un your code, you are doing
But the variable
someVariable
is undefined.The error is not in the code you pasted, I suggest you make a global search for
valueAsNumber
in your codebase and make sure you are calling that on a variable that is notnull