JM4
JM4

Reputation: 6788

While loop within Javascript function

I have the following basic function:

<script type="text/javascript">
function Form_Data(theForm)
{

var t=1;
while (t<=5) {

if (theForm.F[t]FirstName.value == "")
    {
    alert("Please enter Fighter 1's First Name.");
    theForm.F[t]FirstName.focus();
    return (false);
    }
t++;
}

return (true);
}
</script>

The script (js validation) fails using this code. If I remove the [t] and replace with a number (1,2,3,4,etc), the validation works on the appropriate fields. What am I doing wrong?

Upvotes: 0

Views: 1091

Answers (1)

Guffa
Guffa

Reputation: 700372

You can't use an index as part of a name, you have to put the name together as a string and use as index:

theForm['F' + t + 'FirstName']

Upvotes: 3

Related Questions