-1

I need to do auto calculate in FIELD-2 while you type a new value in FIELD-1, but I need it works viceversa also. For example I have a moltiplication factor (25); in FIELD-1 I write 100 and in FIELD-2 will be 2500 (25x100); but if I write 2500 in FIELD-2 I will have 100 in FIELD-1 (2500/25).

This operation must be work if I have the recalculation field already with another number: if I have in FILED-1 100 and in FILED-2 2500 and I re-write 200 in FILED-1 (or 5000 in FILED-2), I will have the new result in the other field.

Sorry for my english and thank you.

Derek 朕會功夫
  • 92,235
  • 44
  • 185
  • 247

1 Answers1

2

With HTML:

<input id="field1">
<input id="field2">

JavaScript:

var fac = 25;

$('#field1').keyup(function() {
    $('#field2').val($('#field1').val() * fac);
});

$('#field2').keyup(function() {
    $('#field1').val($('#field2').val() / fac);
});
Colin vH
  • 527
  • 3
  • 9
  • `parseFloat` is somewhat redundant here since the `*` and `/` operator [will already do that for you](http://www.ecma-international.org/ecma-262/5.1/#sec-11.5). `$('#field1').val()` is also quite inefficient. – Derek 朕會功夫 Nov 29 '14 at 21:27
  • `val()` may be inefficient, but it always works. Removing `parseFloat()`. – Colin vH Nov 29 '14 at 21:31