Click to See Complete Forum and Search --> : Quick Image Calculation


Suamere
06-26-2005, 12:05 PM
I have a bit of code here that I created, and I was hoping someone could tell me where my error is. The goal is to click an image whose value is "x" and the text area value becomes itself + x. The end goal is to have 5 images with 5 different values, and clicking one adds a different amount to the textarea... Here is the code:

<form name=RupCalculate>

<script language="JavaScript"><!--
function RupCalculate()
{
eval("document.RupCalculate.RupTotal.value = '';");
eval("q = document.RupCalculate.GreenRup.value;");
if (q)
{
eval("document.RupCalculate.RupCalculate.value='1'");
}
}
// --></script>

<img src=images/GreenRup.bmp name=GreenRup onclick="RupCalculate();" Value=1><br><br>

<input type=text name=RupTotal value=0 onfocus="this.blur();"><br><br>

</form>



I have also tried input type=image. But that creates a submit type button and changes the page back to itself with a querystring. I want it all to stay on one page, if ya know what I mean. Thanks for any help.

Suamere
06-26-2005, 02:17 PM
I have a better idea, more simplified, yet still not working. Look at this... When I click the buttons "5" and "10" The output becomes "510" and then if I press "5" again, the output becomes "5105" etc... So it is combining strings instead of adding values. Could this be fixed:?



<form name=RupCalculate>

<INPUT TYPE="button" NAME="Rup5" VALUE="5" OnClick="RupCalculate.RupTotal.value = eval(RupCalculate.RupTotal.value + 5)">

<INPUT TYPE="button" NAME="Rup10" VALUE="10" OnClick="RupCalculate.RupTotal.value = eval(RupCalculate.RupTotal.value + 10)">

<INPUT TYPE="button" NAME="Rup20" VALUE="20" OnClick="RupCalculate.RupTotal.value = eval(RupCalculate.RupTotal.value + 20)">

<br><br>

<input type=text name=RupTotal><br><br>

</form>

HaganeNoKokoro
06-26-2005, 02:22 PM
Since RupCalculate.RupTotal.value is a string, it is concatenating instead of adding numerically. Try<INPUT TYPE="button" NAME="Rup5" VALUE="5" OnClick="RupCalculate.RupTotal.value = parseInt(RupCalculate.RupTotal.value)+5">

Suamere
06-26-2005, 03:27 PM
Thank you. That works perfectly. I tried ParseInt before, but it was within the eval, which is probably why it didn't work. Have a good day!