I have an HTML div, which I use JavaScript to retrieve the value from, as below:
var oCreditStatus = document.getElementById('creditStatusField');
var oOrigValue = oCreditStatus.value;
var oOrigValue will always be a number, and sometimes 0 (Zero).
I now want to increment or decrement the number by 1. However, when I do the following:
var oNewValue = oCreditStatus.value + 1;
I get the value 01, then 011, which is wrong as I want 1,2.
I know it is not recognizing it as a number, but how do I get it to do so.
You can't. Javascript engine will read it as a text/string. You've to use following code:
Edit. Use a radix (base 10) Thanks to @James for revision.
var oCreditStatus = document.getElementById('creditStatusField');
var oOrigValue = parseInt(oCreditStatus.value, 10); // use parseFloat for decimal.