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

95
Views
agregando un nuevo valor a la variable

Tengo una pregunta. Tengo JavaScript simple que hace algunas cosas básicas a un número desde la entrada. Tengo una pregunta, ¿cómo puedo crear una variable que siempre rastreará el nuevo valor de entrada, por ejemplo, si ingreso 123 y hago clic en algunos de los siguientes botones, obtengo el resultado, pero si ahora ingreso un nuevo número, por ejemplo, 54321 y vuelvo a hacer clic en algunos de los botones empiezo desde el valor anterior. ¿Cómo puedo hacer que mi variable cambie cada vez que se ingresa o cambia un nuevo valor? Aquí está mi código:

 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

Le sugiero que use un tipo = "número" y escriba el valor en número; ella uso el plus unario para hacerlo

Deberá leer el valor en todas las funciones.

 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

Intenta usar onChange="".

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

Sugeriría, para algo como esto, sería mucho más fácil usar React JS u otro marco con estado.

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!