-2

I'm looking for a solution for the following issue,

I need a Javascript code to calculate this with onekeyup. So for example, if I insert a value of 2 in text1. In text2 the value has to be 200.

      <form>

        <input type='text' name='text1'>

        <input type='text' name='text2'>

        <script>
        var factor= 100;
        var text1= value in text2 * factor;
        var text2= value in text1 / factor;

      </form>

3 Answers3

0

quick and dirty:

<input id="text1" type='text' name='text1' onkeyup="document.getElementById('text2').value = parseInt (this.value) * 100;">
<input id="text2" type='text' name='text2'>
wayneOS
  • 1,427
  • 1
  • 14
  • 20
0

I've stored the empty variables outside the function, and then set the values within the function when the input value has changed.

var factor= 100;
var text1;
var text2;

document.querySelector('input[name="text2"]').onkeyup = function() {
  text1 = document.querySelector('input[name="text2"]').value * factor;
  console.log(text1);
}

document.querySelector('input[name="text1"]').onkeyup = function() {
  text2 = document.querySelector('input[name="text1"]').value / factor;
  console.log(text2)
}
<form>
<input type='text' name='text1'>
<input type='text' name='text2'>
</form>
0

this is the answer.

The HTML

<input type="text" id="valor1">
    <span id="resultado1"></span>
  <input type='text' id="valor2">
  <span id="resultado2"></span>

The JS

$(function()
{
    var factor = 100;
    $("#valor1").on("keyup",function()
    {
    var valor1 = parseInt($("#valor1").val());
        var resultado1 = valor1 * factor;
        $("#resultado1").text(resultado1);
    })
    $("#valor2").on("keyup",function()
    {
    var valor2 = parseInt($("#valor2").val());
        var resultado2 = valor2 / factor;
        $("#resultado2").text(resultado2);
    }) 
})
Alex Hunter
  • 212
  • 9
  • 30