I have a bit of javascript that dynamically multiplies what users are typing in a text field (by the base var), and displays it in a span. Now I’m just trying to figure out how to get the decimal places of the result to float to 2 places, i.e. 10.00 instead of 10
I found the toFixed function, but can’t seem to use it properly… I’d appreciate any help. Thanks
<input id="quantity">
<span id="result"></span>
<script>
window.onload = function() {
var base = 3;
document.getElementById('quantity').onkeyup = function() {
if(this.value.length == 0) {
document.getElementById('result').innerHTML = '';
return;
}
var number = parseInt(this.value);
if(isNaN(number)) return;
document.getElementById('result').innerHTML = number * base;
};
document.getElementById('quantity').onkeyup();
};
1 Answer