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

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}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.