I'm trying to use the following code (found here: Make an html number input always display 2 decimal places) to force number inputs to display two decimal places when the input loses focus/onchange:
function setDecimal(event) {
this.value = parseFloat(this.value).toFixed(2);
}
<input placeholder="$0.00" type="number" onchange="setDecimal" name="amount_20_20_amt" id="amount_20_20_amt" value="" autocomplete="off" min="0" max="50000000" step="0.01">
This doesn't work for some reason, although I was able to use the following inline solution and it works perfectly:
onchange="(function(el){el.value=parseFloat(el.value).toFixed(2);})(this)"
I can't use jQuery or any other libraries (business rules) and I need it to show when the input loses focus. Adjusting the step to ".01" or "any" does not accomplish this.
The JS is stored in a .js file that is loading fine, and everything else in the .js file is working fine, so I think it must be an error in the snippet, although I can't figure out what.
Where am I going wrong?