
Originally Posted by
svoirin
The "addnumb" is 25 and I'm trying to add it to the value of document.getElementById(total).innerHTML with is 1.95, but the alert box just appends the 25 to the end.
As the innerHTML property evaluates to a string, the other operand is type-converted to a string, too. The result is concatenation rather than arithmetic addition.
Code:
function addCostBox(boxName, total, value) {
var element;
if (boxName.checked && document.getElementById
&& (element = document.getElementById(total))) {
alert('This option will add $'
+ (+element.innerHTML + value)
+ ' to your total.');
}
}
The unary plus (+) in the expression,
coerces its operand to a number or NaN (Not-a-Number).
Mike
Bookmarks