skip to Main Content

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


  1. You have not closed script tag

    <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`
        }
    </script> // you are missing this
    

    also add onclick attribute to the button

    Login or Signup to reply.
  2. The error

    TypeError: Cannot set properties of null (setting 'valueAsNumber')
    

    Means that somewhere un your code, you are doing

    someVariable.valueAsNumber = 0; // Or any value
    

    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 not null

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search