0

I'm hoping someone can help, I'm new to Javascript but not HTML/CSS. I'm having a small issue trying to display some Javascript values onto my HTML page. So in a nutshell:

I have two form range sliders the values from these inputs then use JavaScript to work out the total including interest (it's a loan calculator). However what I need is to display the sum at the bottom of the form so:

Amount Borrowed [value] + Interest [value] = Total repay [value]

I need this to update the values as they are sliding the input range, I can't have the user needing to click a button.

Here is my JS:

<script type="text/javascript"><!--
function updatesum() {
document.form.sum.value = (document.form.borrow.value -0 + 5.5) / 100 * 
(document.form.duration.value -0) + (document.form.borrow.value -0 + 5.5);
}
//--></script>

My HTML:

<form name="form" class="form">
    <label for="borrow">How much would you like to <span>borrow</span>?</label>
    <input type="range" name="borrow" id="borrow" value="150" min="1" max="400" onChange="updatesum()" style="color: #1271a9;">

    <label for="duration">How <span>long</span> for?</label>
    <input type="range" name="duration" id="duration" value="18" min="1" max="38" onChange="updatesum()" style="color: #1271a9;">
    <label>Total Repayable:</label>
    <input name="sum" readonly  style="color: #1271a9; font-weight: bold;">
    <p class="sum">Borrowing <div id="sumBorrow"><span>[sum]</span></div> + Interest & fees <span name="duration">[sum]</span> = Total to repay <span name="sum">[sum]</span></p>
  </form>
8
  • looks fine jsfiddle.net/arunpjohny/XHUMR/1 Commented Mar 2, 2014 at 12:15
  • It's the "[sum]" within the <span> tags that I need to update to the values within the inputs, that is the bit I can't seem to get to work. Commented Mar 2, 2014 at 12:30
  • you can use jQuery right? Commented Mar 2, 2014 at 12:31
  • Yes, I can, especially if it makes it work ;) Commented Mar 2, 2014 at 12:36
  • see jsfiddle.net/arunpjohny/XHUMR/2 Commented Mar 2, 2014 at 12:38

1 Answer 1

1

You can use jQuery and some minor html changes

<form name="form" class="form">
    <label for="borrow">How much would you like to <span>borrow</span>?</label>
    <input type="range" name="borrow" id="borrow" value="150" min="1" max="400" onChange="updatesum()" style="color: #1271a9;" />
    <label for="duration">How <span>long</span> for?</label>
    <input type="range" name="duration" id="duration" value="18" min="1" max="38" onChange="updatesum()" style="color: #1271a9;" />
    <label>Total Repayable:</label>
    <input name="sum" id="sum" readonly style="color: #1271a9; font-weight: bold;" />
    <p class="sum">Borrowing
        <div id="sumBorrow"><span class="amount">[sum]</span>
        </div>+ Interest & fees <span class="interest">[sum]</span> = Total to repay <span class="total">[sum]</span>
    </p>
</form>

then

function updatesum() {
    var amount = +$('#borrow').val() || 0,
        duration = +$('#duration').val() || 0,
        interest = (amount + 5.5) / 100 * duration,
        payable = interest + (amount + 5.5);
    $('#sum').val(payable.toFixed(2));
    $('.amount').text(amount.toFixed(2));
    $('.interest').text(interest.toFixed(2));
    $('.total').text(payable.toFixed(2));
}

Demo: Fiddle

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

Comments

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.