Display javascript value in html

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>

Answers


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


Need Your Help

JavaEE6+REST: How do I get all REST resources at runtime?

java web-services rest java-ee glassfish

Is there a possibility to get a list of all registered REST resources at runtime?

How to exchange multidimensional arrays from matlab to python/numpy and back?

python matlab numpy multidimensional-array

i think this code handles n-d correctly (please verify), and i've hacked it to be polymorphic, but it's ugly. is there a better/more efficient way? i'm not sure it shares memory as much as possibl...