Dieter Rausch
01-08-2004, 08:29 PM
Hi JS coders,
I want to add a value - entered into a hidden text box while the page loads - to another value entered after loading the page and get the sum of the two values.
However, it appears that the first entry is taken as a string variable and concatenated to the latter. For example:
The first entry goes into a hidden field:
<input type="hidden" name="slippage" value="5" id="fldSlippage">
The second entry goes manually after loading into this field:
<input type="text" name="tradeEntry" id="fldEntry">
The two are then added together by calling a function which assigns the hidden field's value to a variable as well as the visible field's value to a separate variable by the following code:
var slipFact=document.getElementById("fldSlippage").value;
var tradeEntry=document.getElementById("fldEntry").value;
Finally, a third variable is calcualted by:
var pointsRisk=(tradeEntry+slipFact);
From the answer obtained it is clear that concatenation takes place instead of addition, i.e.
100+5 yields 1005 instead of 105. However, if I replace the "+" sign with a "-" , then 100-5 yields 95, which is correct.
Now, how do I get JS to do an addition instead of concatenation? So far I could not find an answer in various JS tutorials.
Regards
Dieter
I want to add a value - entered into a hidden text box while the page loads - to another value entered after loading the page and get the sum of the two values.
However, it appears that the first entry is taken as a string variable and concatenated to the latter. For example:
The first entry goes into a hidden field:
<input type="hidden" name="slippage" value="5" id="fldSlippage">
The second entry goes manually after loading into this field:
<input type="text" name="tradeEntry" id="fldEntry">
The two are then added together by calling a function which assigns the hidden field's value to a variable as well as the visible field's value to a separate variable by the following code:
var slipFact=document.getElementById("fldSlippage").value;
var tradeEntry=document.getElementById("fldEntry").value;
Finally, a third variable is calcualted by:
var pointsRisk=(tradeEntry+slipFact);
From the answer obtained it is clear that concatenation takes place instead of addition, i.e.
100+5 yields 1005 instead of 105. However, if I replace the "+" sign with a "-" , then 100-5 yields 95, which is correct.
Now, how do I get JS to do an addition instead of concatenation? So far I could not find an answer in various JS tutorials.
Regards
Dieter