-1

This is my first project using JavaScript.
I have a PHP page that loads text fields "if" called for. I thought JavaScript would be a great way to get the total of all Text Fields without Submit.
The code below is an example of my problem.

<SCRIPT language = JavaScript>

function calculate() {
A = document.frmOne.txtFirstNumber.value *1
B = document.frmOne.txtSecondNumber.value *1
C = document.frmOne.txtThirdNumber.value *1
D = A + B + C
document.frmOne.txtFourthNumber.value = D
}

</SCRIPT>

<FORM NAME = frmOne>

Number One: <INPUT TYPE = Text NAME = txtFirstNumber SIZE = 5 value ="">

Number Two: <INPUT TYPE = Text NAME = txtSecondNumber SIZE = 5 value ="">
<P>
Total: <INPUT TYPE = Text NAME = txtFourthNumber SIZE = 5 value = "">
<P>
<Input Type = Button NAME = b1 VALUE = "Add Numbers" onClick = calculate()>

</FORM>

If I add

<INPUT TYPE = Text NAME = txtThirdNumber SIZE = 5 value ="">

to the form, the code works fine.
But again my page dose not always call ALL the Fields in the JS code.

Can anyone help me with this?
Thanks

2
Contributors
3
Replies
6
Views
7 Years
Discussion Span
Last Post by mark2326l
1

You have C = document.frmOne.txtThirdNumber.value *1 in your function so if that field doesn't exist and you try to perform an action on it ( *1 in this case) the function will die.

0

Thanks Shawn
But that didn't fix it.
The only way I can make the Form work the way it is written, is to delete C = document.frmOne.txtThirdNumber.value and +C from D = A + B + C in JS

0

I think I got it

var C ;
if (window.document.frmOne.txtThirdNumber) {C=document.frmOne.txtThirdNumber.value * 1}
else {C=0}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.