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


<FORM NAME = frmOne>

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

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


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?

7 Years
Discussion Span
Last Post by mark2326l

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.


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


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.