Click to See Complete Forum and Search --> : focus not working


florida
04-07-2003, 05:56 AM
Please advise How I can get my focus to work. The below checks for blank spaces and does work but cant get the focus to work in this for loop.



function checkData()
{
myFields = new Array(document.myform.fieldOne.value,document.myform.fieldTwo.value);
myValues = new Array("FieldOne", "FieldTwo");
myReg = /^\s*$/;


myInfo = "";
for (i=0; i<myFields.length; i++)
{
if( myReg.test(myFields[i]) )
{
myInfo += "\n" + myValues[i];
}
}

if (myInfo != '')
{

myInfo += "\n\Enter required values.";
alert(myInfo);
document.myform.focus();
return false;
}

}

gil davis
04-07-2003, 06:07 AM
You need to focus on a specific field in the form, not the form itself.

florida
04-07-2003, 06:41 AM
I tried this but it doesnt work:

document.myform.myFields[i].focus();

gil davis
04-07-2003, 07:01 AM
myFields[i] is a string value, not a form field. If you change your array to be the text boxes, not the values, you can then use focus().myFields = new Array(document.myform.fieldOne,document.myform.fieldTwo);
...
if (myReg.test(myFields[i].value))
...
myFields[i].focus();
...

florida
04-07-2003, 11:25 AM
Thanks Gil for solving my problem. It now works.


You said:
"If you change your array to be the text boxes, not the values"

Please explain (if you have the time) the difference between "text boxes" and "values".

I thought they were the same??

gil davis
04-07-2003, 12:46 PM
In your original post, the array contained only the values of the text boxes (what the user entered) (document.myform.fieldOne.value). This is simply a string and has no other properties or methods.

In my example, the array contained a pointer to the text box itself (document.myform.fieldOne). As such, it has all the methods and properties of the text box, including the value and focus().

florida
04-07-2003, 01:14 PM
Thanks (again)!!

Now I understand it as when I use "document.myform.fieldOne" it actually contains a pointer with all the text box properties.