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

117
Visualizações
HTML Input to number and add

I tried to make an input and whatever the number is there, to add it using a button. I cannot understand what my code miss and why is not adding the value from the input.

Do I need to convert to a number from a string or?

let funds = document.getElementById("funds");
let input = document.getElementById("input").value;
let add = document.getElementById("add");
let base = 0;

function adauga() {
  funds.innerText = base += input;
}
<p>Funds Available</p>
<p id="funds">0$</p>
<input id="input" type="number" class="text-black w-28 rounded-xl border-2 border-red-700" />
<button id="add" onclick="adauga()" class="ml-4 bg-red-700 px-3 py-1 rounded-xl">Add</button>

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

0

Instead of

let input = document.getElementById("input").value;

You should be using

let input = document.getElementById("input");

and get the current input value with

funds.innerText = (base+= input.valueAsNumber)+"$";

about 4 years ago · Juan Pablo Isaza Relatório

0

Two issues.

The first is, as you correctly pointed out, that the number needs to be coerced to a string.

The second is that you're immediately setting the value of input to that element's value rather than using the new value of the input box to change the text content. So pick up the value in the function instead.

let funds = document.getElementById("funds");
let input = document.getElementById("input");
let add = document.getElementById("add");

let base = 0;

function adauga() {
  funds.textContent = `${(base += Number(input.value))}$`;
}
<p>Funds Available</p>
<p id="funds">0$</p>
<input id="input" type="number" class="text-black w-28 rounded-xl border-2 border-red-700" />
<button id="add" onclick="adauga()" class="ml-4 bg-red-700 px-3 py-1 rounded-xl">Add</button>

Additional documentation

  • Template literals
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