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

104
Views
adding new value to variable

I have a question I have simple JavaScript that do some basic stuff to a number from input. I have a question how can I make variable that will always track the new input value for example if I enter 123 and click on some of the following buttons I get the result, but if I now enter new number for example 54321 and click again on some of the buttons I start from the previous value. How can I make my variable change every time a new value is entered or changed ? Here is my code:

var number = document.getElementById("number");
var numberValue = number.value;
console.log(numberValue);

function plus() {
  number.value = ++numberValue;
}

function minus() {
  number.value = --numberValue;
}

function flip() {
  var temp = numberValue;
  var cifra, prevrten = 0;
  while (temp > 0) {
    cifra = temp % 10;
    prevrten = (prevrten * 10) + cifra;
    temp = temp / 10 | 0;
  }
  number.value = prevrten;
}
window.onload = function() {
  number.value = "";
}
<div>
  <input type="text" id="number" id="output" onload="restart();">
  <input type="button" value="<" onclick="minus();">
  <input type="button" value=">" onclick="plus();">
  <input type="button" value="FLIP" onclick="flip();">
  <input type="button" value="STORE" onclick="store();">
  <input type="button" value="CHECK" onclick="check();">
</div>

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

I suggest you use a type="number" and case the value to number - her I use the unary plus to do so

You will need to read the value in all functions

let numberValue = 0;

function store() {}
function check() {}

function plus() {
  numberValue = +number.value;
  number.value = ++numberValue;
}

function minus() {
  numberValue = +number.value;
  number.value = --numberValue;
}

function flip() {
  let numberValue = +number.value;
  var cifra, prevrten = 0;
  while (numberValue > 0) {
    cifra = numberValue % 10;
    prevrten = (prevrten * 10) + cifra;
    numberValue = numberValue / 10 | 0;
  }
  number.value = prevrten;

}

window.addEventListener("load", function() {
  let number = document.getElementById("number");
  number.value = 0;
})
<div>
  <input type="number" id="number" id="output" onload="restart();">
  <input type="button" value="<" onclick="minus();">
  <input type="button" value=">" onclick="plus();">
  <input type="button" value="FLIP" onclick="flip();">
  <input type="button" value="STORE" onclick="store();">
  <input type="button" value="CHECK" onclick="check();">
</div>

about 4 years ago · Juan Pablo Isaza Report

0

Try using onChange="".

<input type="text" id="number" id="output" onload="restart();" onChange="updateVal();">
function updateVal() {
  numberValue = number.value;
}

I would suggest, for something like this, it would be much easier to use React JS or another framework with state.

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!