Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

243
Visualizações
parseInt return NaN when take value from input box

i am trying to add 2 number as value from input form. typeof(parseInt(value1)) and typeof(parseInt(value2)) return number but parseInt(value1)+parseInt(value2) return NaN. please someone explain

var input1 = document.getElementById('num1')
var input2 = document.getElementById('num2')
var value1 = input1.value
var value2 = input2.value
btn.addEventListener('click', myfunc)
function myfunc() {
    
    alert(typeof(parseInt(value1)))
    alert(typeof(parseInt(value2)))
    alert(parseInt(value1)+parseInt(value2))
}
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Tính toán</title>
  </head>
  <body>
    <form action="">
      <div>
        <label for="num1">Number 1</label>
        <div>
          <input type="number" name="num1" id="num1" />
          <span class="error" id="errNum1">(*)</span>
        </div>
      </div>
      <div>
        <label for="num2">Number 2</label>
        <div>
          <input type="number" name="num2" id="num2" />
          <span class="error" id="errNum2">(*)</span>
        </div>
      </div>
      <div></div>
      <div>
        <input type="button" value="Tính toán" id="btn" />
      </div>
    </form>
    <script src="js.js"></script>
  </body>
</html>

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You get the value of the inputs when the page is loaded, so they will always be empty. Instead you want to get the values when the button is clicked.

btn.addEventListener('click', myfunc)
function myfunc() {
    var input1 = document.getElementById('num1')
    var input2 = document.getElementById('num2')
    var value1 = input1.value
    var value2 = input2.value
    alert(typeof(parseInt(value1)))
    alert(typeof(parseInt(value2)))
    alert(parseInt(value1)+parseInt(value2))
}
about 4 years ago · Juan Pablo Isaza Relatório

0

The correct way to do that:

You don't need to use parseInt() on input type= number,
just use valueAsNumber property

const myForm = document.forms['my-form']

myForm.btn.onclick = evt =>
  {
  console.clear()
  console.log(typeof myForm.num1.valueAsNumber)
  console.log(typeof myForm.num2.valueAsNumber)
  console.log( myForm.num1.valueAsNumber + myForm.num2.valueAsNumber )
  }
label
, button {
  display : block;
  margin : .3em;
  }
<form action="" name='my-form'>
  <label>
    Number 1
    <input type="number" name="num1" value="0" >
  </label>
  <label>
    Number 2
    <input type="number" name="num2" value="0" >
  </label>

  <button name="btn" type="button"> Tính toán </button>

</form>

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda