How to solve the problem , the value of the variable I receive from the HTML Input gives me the Nan value

Asked

Viewed 38 times

0

function InicirContagem(){
    var vinicio = document.getElementById('txtInicio')
    var res = document.getElementById('Resposta')
    //var vn = Number.parseInt(vinicio);
    res.innerHTML=` era para aparecer o valor aqui: ${vinicio}`
}
<!DOCTYPE html>
<html lang="pt-br">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Modelo de Exercìcio</title>
    <link rel="stylesheet" href="estilo016.css">
</head>

<body>
    <header>
        <h1> Contar Números </h1>
    </header>
    <section>
        
           inicio : <input type="number" id="txtinicio" min = '0'>
           <input type="button" value="Verificar" onclick="InicirContagem()">
           
    
        
        <div id = "Resposta">
                Preparando a contagem...

        </div>
    </section>
    <footer> <p>&copy; GrauTécnico INFO 03.</p>
    </footer>
    <script language="javascript" type="text/javascript"  src="script016.js"></script>
    
</body>
</html>

  • Is appearing null and not Nan.

1 answer

0

In its "Inicircontage" function, there is a problem when picking the input value, because the field name is different from the form ID. Change the function to look like this:

function InicirContagem(){
    var vinicio = document.getElementById('txtinicio').value;
    var res = document.getElementById('Resposta')
    //var vn = Number.parseInt(vinicio);
    res.innerHTML=` era para aparecer o valor aqui: ${vinicio}`
}

  • Now fixed and appears this way res.innerHTML=` was to appear the value here: [Object Htmlinputelement]

  • @maykon you must have forgotten to put .value in the var vinicio = document.getElementById('txtinicio').value;.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.