0

This is my first attempt at using jQuery. I'm attempting to implementing a basic finance calculator.

I'm using jQuery UI range sliders. The script for these is as follows:

jQuery( document ).ready(function( $ ) {
    $( "#slider-deposit" ).slider({
      value: 100,
      min: <?php echo json_encode($minDeposit); ?>,
      max: <?php echo json_encode($maxDeposit); ?>,
      step: 1,
      slide: function( event, ui ) {
        $( "#amount-deposit" ).val( "" + ui.value );
      }
    });
    $( "#amount-deposit" ).val( "" + $( "#slider-deposit" ).slider( "value" ) );
  });  

    jQuery( document ).ready(function( $ ) {
        $( "#slider-payback" ).slider({
      value:100,
      min: 12,
      max: 36,
      step: 12,
      slide: function( event, ui ) {
        $( "#payback-period" ).val( "" + ui.value );
      }
    });
    $( "#payback-period" ).val( "" + $( "#slider-payback" ).slider( "value" ) );
  });  

I also have several text fields on the page, I'm attempting to update these text fields dynamically as the values of the range sliders change on the page.

Here's one text field ax an exmaple:

<input type="text" id="monthly-repayments" readonly style="border:0; color:#f6931f;"> 

What is the best way to ad a value to these text fields and change it dynamically as the values of the sliders change?

Thanks

Here is the full html:

  <div>
    <label for="amount-depoist">Deposit amount:</label>
    <input type="text" id="amount-deposit" readonly style="border:0; color:#f6931f;">
    <div id="slider-deposit"></div>
</div>

<div>
  <label for="payback-period">Payback Period:</label>
  <input type="text" id="payback-period" readonly style="border:0; color:#f6931f;">
<div id="slider-payback"></div>
<div>

<div>
    <label for="apr_rate">APR:</label>
    <input type="text" id="apr_rate" readonly style="border:0; color:#f6931f;">
</div>

<div>
    <label for="monthly-repayments">Monthly Payments:</label>
    <input type="text" id="monthly-repayments" readonly style="border:0; color:#f6931f;">
</div>

<div>
    <label for="total_payable">Total Payable:</label>
    <input type="text" id="total_payable" readonly style="border:0; color:#f6931f;">
</div>
</div>
5
  • Can you create a JSfiddle? Commented Nov 22, 2015 at 19:27
  • Sure, might take me a few minutes Commented Nov 22, 2015 at 19:28
  • Just because you ask the best way. I'll recommend you to use angular js, because it is design to support two way binding. Look like it suit your requirment Commented Nov 22, 2015 at 19:28
  • What script does your browser see, because I'm certain that it doesn't – and certainly shouldn't – see any of the <?php...?> statements; what values do those calls resolve to? Commented Nov 22, 2015 at 19:31
  • Sorry, there are some php elements I left out because I didn't think they were necessary . I can add them in in an edit if you like? They are just integer values that I'm passing in to the script. Commented Nov 22, 2015 at 19:34

1 Answer 1

1

Add an extra function in the slide event change function so that it updates the value of the input field - i.e. something like this:

Change:

  slide: function( event, ui ) {
    $( "#payback-period" ).val( "" + ui.value );
  }

to

  slide: function( event, ui ) {
    $( "#payback-period" ).val( "" + ui.value );
    $('#monthly-repayments').val( "" + ui.value );
  }

This should update the value of the #monthly-repayments input form each time the value of the slider changes

Sign up to request clarification or add additional context in comments.

8 Comments

Ok thanks, how might I do some arithmetic against these values though? For example how might I get the value of 'amount-deposit' and multiply it by 'monthly-repayments' in that slide function event. Thanks
@LiamFell are amount-deposit and monthly-repayments JS variables or do you need to parse them from somewhere?
They are JS variables. Thanks
var total = amount-deposit * monthly-repayments and then update the following: $('#monthly-repayments').val( total )
Was that what you wanted?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.