Forum Moderators: open

Message Too Old, No Replies

Calculate my amount

Dynamically calculates from my text box

         

Shuvi

8:25 pm on Mar 2, 2004 (gmt 0)

10+ Year Member



Does some one know a JS that I can use that takes the values a user enters for totals the amount at the bottom. I have five possible text boxes user can enter values into. I would like to see the total as they enter.
Any help is appreciated!

korkus2000

9:05 pm on Mar 2, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I think you will need to call a calculating function in the onblur event handler of the text box.

<input type="text" onblur="calculate()" id="textbox1">

Shuvi

9:16 pm on Mar 2, 2004 (gmt 0)

10+ Year Member



Can you give me more details? Please?

korkus2000

9:33 pm on Mar 2, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



here is a basic script you could modify

<script>
function calculate(){
if(isNaN(document.test.textbox1.value) ¦¦ document.test.textbox1.value==""){
var text1 = 0;
}else{
var text1 = parseInt(document.test.textbox1.value);
}
if(isNaN(document.test.textbox2.value) ¦¦ document.test.textbox2.value==""){
var text2 = 0;
}else{
var text2 = parseInt(document.test.textbox2.value);
}
if(isNaN(document.test.textbox3.value) ¦¦ document.test.textbox3.value==""){
var text3 = 0;
}else{
var text3 = parseInt(document.test.textbox3.value);
}
if(isNaN(document.test.textbox4.value) ¦¦ document.test.textbox4.value==""){
var text4 = 0;
}else{
var text4 = parseInt(document.test.textbox4.value);
}
document.test.textbox5.value = (text1+text2+text3+text4);
}
</script>
<form name="test">
<input type="text" onblur="calculate()" name="textbox1"> <br>
<input type="text" onblur="calculate()" name="textbox2"><br>
<input type="text" onblur="calculate()" name="textbox3"><br>
<input type="text" onblur="calculate()" name="textbox4"><br>
<br>
answer<br>
<input type="text" name="textbox5"><br>
</form>

Change the broken pipe characters to regular pipe characters. It will not see these as or and will throw exceptions.

Shuvi

9:47 pm on Mar 2, 2004 (gmt 0)

10+ Year Member



Thank you!

Shuvi

6:32 pm on Mar 3, 2004 (gmt 0)

10+ Year Member



I tried this but it's not working. At the botton in the status bar it's giving me a exclamation for error.
Not sure what is going on.

mylungsarempty

8:56 pm on Mar 3, 2004 (gmt 0)

10+ Year Member



Double click on the exclaimation error icon... what does it tell you in the box that appears, exactly?

Shuvi

9:24 pm on Mar 3, 2004 (gmt 0)

10+ Year Member



My mistake. It's fixed!
Thank you. But I didn't know that if you double click the exclamation it would tell you the error. Thanks for the tip!

Shuvi

9:43 pm on Mar 9, 2004 (gmt 0)

10+ Year Member



I just noticed that it doesn't calculate decimal places. Can anyone help?

Shuvi

9:45 pm on Mar 9, 2004 (gmt 0)

10+ Year Member



Nevermind I just changed it to ParseFloat.
Ignore my last entry.

Shuvi

6:08 pm on Mar 12, 2004 (gmt 0)

10+ Year Member



Is there a way to limit the number of decimal places it goes till? Is there anything called ParseCurrency?

ajkimoto

9:02 pm on Mar 12, 2004 (gmt 0)

10+ Year Member



Shuvi,

This was just addressed in this group a couple of days ago. Here is a link to the thread--two alternate answers can be found in #6 and #7.

[webmasterworld.com...]

ajkimoto