Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

111
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!