How to change the product of a calculation during a "slide" event with JQuery UI slider

How to change the product of a calculation during a "slide" event with JQuery UI slider

Hi there

I've got a UI Slider in a site that allows the user to set one of a number of values that are then calculated through the JS script. I've created the function 'calculate' for this. 

I would like 'calculate' to occur on the slide event, so the product of the calculation changes on screen as the user moves the slider.

I thought this might relate to the order in which I call the scripts, but in the console it looks like the 'calculate' function is simply not happening, which makes me think the problem lies in the .slide code. 'Calculate' does work when I call it elsewhere in the program.

The main.js file contains:

  1. $(document).ready( function() {
  2.     $('#simpslider2').slider({
  3.       min: 0,
  4.       max: 100,
  5.       value: 25,
  6.       step: 1,
  7.       slide: function( event, ui ) {
  8.         $( "#simpvalue2" ).val( ui.value );
  9.         calculate();
  10.       }
  11.     });
  12. //    $( "#simpvalue2" ).val( $( "#simpslider2" ).slider( "value" ));
  13. });

The calculate code is:

  1. function calculate(){
  2.     console.log('I am here!!')
  3.     var amount = parseInt($("#amount2").val(), 10);
  4.         var f = $("#finrtn2").val();
  5.         var finRtn = (f/100)
  6.         var c = $("#cost2").val();
  7.         var cost = (c/100)
  8.         var duration = parseInt($("#duration2").val(), 10);
  9.         var yearsPerGrant = parseInt($("#yearspergrant2").val(), 10);
  10.         var simp = parseInt($("#simpvalue2").val(), 10);
  11.         var netRtn = (finRtn - cost);
  12.         var temp = (1 + netRtn);
  13.         var totalInvSize = amount * Math.pow(temp, duration);
  14.         var socImpGrant = (((amount * (Math.pow(temp, yearsPerGrant))) - amount) * (duration / yearsPerGrant) + amount);
  15.         var socImpInv = (amount * (simp/100));
  16.         var socialImpactPA = (socImpGrant + socImpInv);
  17.         $("#result2").html(socialImpactPA.toFixed(0));

And the html is:

  1. <div class="form-group">
  2.               <label class="control-label col-md-1" for="simpvalue2">SIMP</label> 
  3.               <span class="col-md-1"></span>
  4.               <span class="badge col-md-1" data-toggle="modal" data-target="#myModal">?</span>
  5.               <span class="col-md-1"></span>
  6.               <script language="javascript" type="text/javascript" src="jquery-1.11.2.js"></script>
  7.               <script type= "text/javascript"src="calculate2.js"></script>
  8.               <div class="col-md-6" id="simpslider2" >
  9.                 </div>
  10.                 <div class="col-md-0.6"></div>
  11.                 <input class="col-md-1" id="simpvalue2" type="text" readonly style="border:0; color:#f6931f; font-weight:bold; font-size: 1.2em; background-color:C0FFED"></input>
  12.                 <br>
  13.                 <br>
  14.                 <input class="btn btn-default" type="button" id="calculate2" value="Calculate" />
  15.                 <input class="btn btn-default" type="button" id="refresh2" value="Reset" />
  16.               </div>
  17.              <br>       
  18.              <div class="well">The social impact of this investment is:
  19.               <font size="32"><div id="result2"></div></font>
  20.              </div>
  21.             </div>

I'd be really grateful if anyone is able to advise!

Thanks

Polly