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

229
Visualizações
How do I access and update value in HTML input type number?

I am trying to perform a calculation where the number entered by the user in the first input is returned in the readonly second input field instantly. Its a simple calculation to take 20% of the first number and add it back to the first number, to be returned in the second number field. (e.g 20% transfer fees on $100 would return $120.

It seems I am able to access the values in the input fields, but the returned calculation is not returning the correct values. If 40 is entered in the first input, the returned second input displays 408 instead of 48. Can someone help?

function myFunc() {
  const transferamount = document.getElementById("sendamount").value;
  document.getElementById("totalled").value = transferamount + (0.20 * transferamount);
}
<form>
  <input type="number" id="sendamount" value="" name="amounttosend" oninput="myFunc()" required>

  <input type="text" id="totalled" value="" readonly>
</form>

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

0

the value you get from an input is always considered as a string, that's why your calculations are incorrect. To perform as expected you need to convert your input to an int or a float using parseInt() or parseFloat()

const transferamount = parseInt(document.getElementById("sendamount").value);
about 4 years ago · Juan Pablo Isaza Relatório

0

the calculus is slightly wrong ^^

Simply change

document.getElementById("totalled").value = transferamount + (0.20 * transferamount);

To

new_amount = transferamount * 1.20;
document.getElementById("totalled").value = new_amount;

Here is the edited code

function myFunc() {
    const transferamount = document.getElementById("sendamount").value;
    new_amount = transferamount * 1.20;
    document.getElementById("totalled").value = new_amount;
}
<form>
  <input type="number" id="sendamount" value="" name="amounttosend" oninput="myFunc()" required>

  <input type="text" id="totalled" value="" readonly>
</form>

Have a nice day !

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