-1

My question is pretty much simple. What i want is exactly as i described on title. I have 2 input fields (field1 and field2). When i add a number in the field1 i need to divide it with a specific number (eg 1.38) and populate the result in the field2 automatically.

Right now i am using this code to populate the data form field1 to field2 but i dont know how can i perform the division math calculation

<input type='text' id='field_1'>
<input type='text' id='field_2'>

$(document).ready(function () {
$('#field_1').on('change', function (e) {
$('#field_2').val($('#field_1').val());
});
});

Here is the JSFiddle

Marsel
  • 323
  • 2
  • 11
  • Downvote? i just wondering why !! – Marsel Feb 18 '17 at 07:39
  • 1
    Oh come on. If any tutorial doesn't cover basic arithmetics, [Google does](https://www.google.com/search?q=how+to+divide+in+javascript). – JJJ Feb 18 '17 at 07:41
  • I'm not sure why you were down voted either. It is a basic question but everyone has to start somewhere. ¯\_(ツ)_/¯ – Daniel Smith Feb 18 '17 at 08:00
  • Thank @DanielSmith i think also that other people might find my question helpfull. Maybe Stackoverflow must be used only by expert programmers and not newbies like me – Marsel Feb 18 '17 at 08:05

1 Answers1

2

That is a pretty basic programming operation, however, code below as follows:

$(document).ready(function () {
  $('#field_1').on('change', function (e) {
    var result = parseFloat($(this).val()) / 1.38; // Change 1.38 value with the desired number
    $('#field_2').val(result);
  });
});
Den Isahac
  • 1,335
  • 11
  • 26