Simple Division on "Form" with Javascript -
i'm trying thought simple, apparently i'm having trouble doing it.
i have 2 input fields on page ('sales', 'fees'). third input field should value of 'fees' divided 'sales'.
one caveat: don't want standard form have "submit" it. want automatically update value (least amount of effort on users part).
<script type="text/javascript"> var sale = document.getelementbyid('sales').value; var fee = document.getelementbyid('fees').value; document.getelementbyid('rate').value = sale / fee; </script>
and
<p>total purchases: <input type="text" name="sales" id="sales" /></p> <p>total fees: <input type="text" name="fees" id="fees" /></p> <p>your net rate is: <input type="text" name="rate" id="rate" /></p>
i'm assuming there way set value="..."
id="rate"
, i'm off-base attempt, i'm hoping might able me out this.
thanks!
you can either set on blur
event or similar, can run on interval update automatically. change 100
in setinterval
call larger number see take time refresh.
the javascript had ran once on page load, , it.
i added basic error checking.
function calculatenetrate(){ var sale = document.getelementbyid('sales').value; var fee = document.getelementbyid('fees').value; sale = isnan(sale) ? 0 : sale; fee = isnan(fee) ? 0 : fee; var netrate = fee > 0 ? sale / fee : nan; document.getelementbyid('rate').value = isnan(netrate) ? 0 : netrate; } setinterval(function(){ calculatenetrate(); }, 100);
<p>total purchases: <input type="text" name="sales" id="sales" /></p> <p>total fees: <input type="text" name="fees" id="fees" /></p> <p>your net rate is: <input type="text" name="rate" id="rate" /></p>
Comments
Post a Comment