user6570896
user6570896

Reputation: 3

jQuery auto populate input field based on value from other input field

I'm creating a loan calculator. I was trying to figure out how to auto-populate an interest rate field based loan amount entered.

Here's a sample scenario:

Here's my markup:

<input type="text" size="12" id="loanamount" name="loanamount"/>
<input type="text" size="2" name="tenure" />
<input type="text" size="4" id="interestrate" name="interestrate" disabled/>

Hope somebody can help me up.

Thanks.

FD

Upvotes: 0

Views: 3192

Answers (2)

Mark
Mark

Reputation: 5088

Listen for the keyup, calculate the rate, then set the value of the interest rate input:

 $("#loanamount").keyup(
   function (e) {
      var loan = $(e.currentTarget).val();
      if (loan <= 100000) {
        $('#interestrate').val('4.5');
      } else if (loan <= 500000) {
        $('#interestrate').val('4.4');
      } else if (loan <= 1000000) {
        $('#interestrate').val('4.3');
      } else if (loan >= 1000001) {
        $('#interestrate').val('4.0');
      } else {
        $('#interestrate').val('');
      }
   }
 );

Fiddle is here

Upvotes: 3

Shlomi Haver
Shlomi Haver

Reputation: 974

You can bind your input field to keyup event which fire every time you type something and then get the amount and set the interest rate accordingly

Here is an example:

$("loanamount").on( "keyup", function(e){
   var amount = e.target.value;
   var intrestrate = 0;
   if(amount > 0)
     intrestrate = 4.5;
   else
      //... continue with conditions
   $('#interestrate').val(intrestrate);
})

Upvotes: 1

Related Questions