ruby on rails - Javascript for simple interest formula -


i totally new on javascript, had written following script in have calculate simple interest. script not working. in loan_request_amount principle, loan_request_no_of_emi time, loan_type_interest_rate rate .

 <script type="text/javascript">   $(function(){     $('#loan_request_amount').on('input', function() {       var loan_request_amount = parsefloat($('#loan_request_amount').val());       var loan_request_no_of_emi = parsefloat($('#loan_request_no_of_emi').val());       var loan_type_interest_rate = parsefloat($('#loan_type_interest_rate').val());        var ans="";       if (isnan(loan_request_amount) || isnan(loan_request_no_of_emi) || isnan(loan_type_interest_rate)) {         ans = ""       }       else{         ans = (loan_request_amount * loan_request_no_of_emi * (loan_type_interest_rate/12))/100.tofixed(2);       }       $('#loan_request_interest_amount').val(ans);      });      $('#loan_request_no_of_emi').on('input', function() {       var loan_request_no_of_emi = parsefloat($('#loan_request_no_of_emi').val());       var loan_request_amount = parsefloat($('#loan_request_amount').val());       var loan_type_interest_rate = parsefloat($('#loan_type_interest_rate').val());        var ans="";       if (isnan(loan_request_amount) || isnan(loan_request_no_of_emi) || isnan(loan_type_interest_rate)) {         ans = ""       }       else{         ans = (loan_request_amount * loan_request_no_of_emi * (loan_type_interest_rate/12))/100.tofixed(2);       }       $('#loan_request_interest_amount').val(ans);     });      $('#loan_type_interest_rate').on('input', function() {       var loan_type_interest_rate = parsefloat($('#loan_type_interest_rate').val());       var loan_request_no_of_emi = parsefloat($('#loan_request_no_of_emi').val());       var loan_request_amount = parsefloat($('#loan_request_amount').val());        var ans="";       if (isnan(loan_request_amount) || isnan(loan_request_no_of_emi) || isnan(loan_type_interest_rate)) {         ans = ""       }       else{         ans = (loan_request_amount * loan_request_no_of_emi * (loan_type_interest_rate/12))/100.tofixed(2);       }       $('#loan_request_interest_amount').val(ans);     });   });   </script> 

my entire form -

<div class="box box-default">   <div class="box-header with-border">     <h3 class="box-title">loan request</h3>     <div class="box-body">       <%= bootstrap_form_for(@loan_request,html: {id: 'loan_request'}) |f| %>       <div class="row">         <div class="col-sm-3">           <div class="form-group required">             <%= f.select :membership_id,all_membership,{label: 'member',include_blank: 'select member'} %>           </div>         </div>          <div class="col-sm-3">           <div class="form-group required">             <%= f.select :loan_type_id,all_loan_type,label: 'loan type',include_blank: 'select loan type'%>           </div>         </div>           <div class="col-sm-3">           <div class="form-group required">             <%= f.text_field :amount,label: 'amount', id: "loan_request_amount" %>           </div>         </div>          <div class="col-sm-3">           <div class="form-group required">             <%= f.text_field :no_of_emi,label: 'no. of emi', id: "loan_request_no_of_emi"%>           </div>         </div>      </div>     <div class="row">         <div class="col-sm-3">           <div class="field">             <%= f.text_field :emi,label: 'emi'%>           </div>         </div>           <div class="col-sm-3">           <div class="field">             <%= f.text_field :interest_amount,label: 'interest amount' , disabled_id: "loan_request_interest_amount" %>           </div>         </div>           <div class="col-sm-3">           <div class="field">            <div class="input-group">             <div class="input-group-addon">              <i class="fa fa-calendar"></i>             </div>             <div class="form-group required">               <%= f.text_field :date, label: 'date', class: 'loan_request'%>             </div>            </div>           </div>         </div>          <div class="col-sm-3">           <div class="actions" style="padding: 20px">             <%= f.submit'create loan request', :class => class_name, data: { disable_with: data_info } %> <!-- | < link_to 'back', loan_requests_path,class:'btn btn-sm btn-default fa fa-arrow-left' %> -->           </div>         </div>     </div>     </div>   </div> <% end %> </div>    <script type="text/javascript">   $(function(){     $('#loan_request_amount').on('input', function() {       var loan_request_amount = parsefloat($('#loan_request_amount').val());       var loan_request_no_of_emi = parsefloat($('#loan_request_no_of_emi').val());       var ans="";       if (isnan(loan_request_amount) || isnan(loan_request_no_of_emi)) {         ans = ""       }       else{         ans = (loan_request_amount / loan_request_no_of_emi).tofixed(2);       }       $('#loan_request_emi').val(ans);      });      $('#loan_request_no_of_emi').on('input', function() {       var loan_request_no_of_emi = parsefloat($('#loan_request_no_of_emi').val());       var loan_request_amount = parsefloat($('#loan_request_amount').val());       var ans="";       if (isnan(loan_request_amount) || isnan(loan_request_no_of_emi)) {         ans = ""       }       else{         ans = (loan_request_amount / loan_request_no_of_emi).tofixed(2);       }       $('#loan_request_emi').val(ans);     });   });   </script>  <script type="text/javascript">    $(function(){     $('#loan_request_amount').on('input', function() {       var loan_request_amount = parsefloat($('#loan_request_amount').val());       var loan_request_no_of_emi = parsefloat($('#loan_request_no_of_emi').val());       var loan_type_interest_rate = parsefloat($('#loan_type_interest_rate').val());        var ans="";       if (isnan(loan_request_amount) || isnan(loan_request_no_of_emi) || isnan(loan_type_interest_rate)) {         ans = ""       }       else{         ans = ((loan_request_amount * loan_request_no_of_emi * (loan_type_interest_rate/12))/100).tofixed(2);       }       $('#loan_request_interest_amount').val(ans);      });      $('#loan_request_no_of_emi').on('input', function() {       var loan_request_no_of_emi = parsefloat($('#loan_request_no_of_emi').val());       var loan_request_amount = parsefloat($('#loan_request_amount').val());       var loan_type_interest_rate = parsefloat($('#loan_type_interest_rate').val());        var ans="";       if (isnan(loan_request_amount) || isnan(loan_request_no_of_emi) || isnan(loan_type_interest_rate)) {         ans = ""       }       else{         ans = ((loan_request_amount * loan_request_no_of_emi * (loan_type_interest_rate/12))/100).tofixed(2);       }       $('#loan_request_interest_amount').val(ans);     });      $('#loan_type_interest_rate').on('input', function() {       var loan_type_interest_rate = parsefloat($('#loan_type_interest_rate').val());       var loan_request_no_of_emi = parsefloat($('#loan_request_no_of_emi').val());       var loan_request_amount = parsefloat($('#loan_request_amount').val());        var ans="";       if (isnan(loan_request_amount) || isnan(loan_request_no_of_emi) || isnan(loan_type_interest_rate)) {         ans = ""       }       else{         ans = ((loan_request_amount * loan_request_no_of_emi * (loan_type_interest_rate/12))/100).tofixed(2);       }       $('#loan_request_interest_amount').val(ans);    });   });   </script>     <script type="text/javascript">   $(function(){     $('.loan_request').datepicker({     changeyear:true,     changemonth: true,     yearrange: '-200:-0',     dateformat: 'dd-mm-yy' });      }); </script>  <script> $("#loan_request").validate({     rules: {       "loan_request[membership_id]":{         required: true,       },       "loan_request[amount]":{         required: true,         numbersonly: true,       },       "loan_request[loan_type_id]":{         required: true,       },       "loan_request[no_of_emi]":{         required: true,         numbersonly: true,       },       "loan_request[date]":{         required: true,       }     },     messages: {       "loan_request[membership_id]":{         required: "please select member",       },       "loan_request[amount]":{         required: "please specify amount",       },       "loan_request[loan_type_id]":{         required: "please select loan type  ",       },       "loan_request[no_of_emi]":{         required: "please specify no. of emi  ",       },       "loan_request[date]":{         required: "please select date  ",       }     },     errorplacement: function(error, element) {      error.css({"color": "red", "font-size": "12px","font-weight" : "normal"})      error.insertafter(element.parent(element));     }    }); </script> 

you have error in javascript. missing parenthese causes error:

 ans = ((loan_request_amount * loan_request_no_of_emi * (loan_type_interest_rate/12))/100).tofixed(2); 

before method tofixed, there no parenthese.

a complete , compact solution is:

$("#loan_request_amount,#loan_request_no_of_emi,#loan_type_interest_rate").on('input', function() {    var loan_request_amount = parsefloat($('#loan_request_amount').val());   var loan_request_no_of_emi = parsefloat($('#loan_request_no_of_emi').val());   var loan_type_interest_rate = parsefloat($('#loan_type_interest_rate').val());    var ans=(loan_request_amount * loan_request_no_of_emi * (loan_type_interest_rate/12))/100;   if (isnan(ans)) {     ans = ""   }   else{     ans =  ans.tofixed(2);   }   $('#loan_request_interest_amount').val(ans);  }); 

jsfiddle


Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -