The SitePoint Forums have moved.

You can now find them here.
This forum is now closed to new posts, but you can browse existing content.
You can find out more information about the move and how to open a new account (if necessary) here.
If you get stuck you can get support by emailing forums@sitepoint.com

If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

field focus problem

Any ideas here would be appreciated. I'm stumped.

I have a script that runs when the page is loaded to set focus to a field. If I have an alert immediately before I call focus() on the field everything is good. If the alert isn't there, the cursor never shows up in the field...however, it scrolls to the textbox and if I hit tab I move to the field immediately following it which would indicate I really do have focus on the field, yet the cursor isn't there so I can't start typing in the field. I thought it might be an issue with the page not being fully loaded but if that is the case why does it scroll down to the field and move to the next field after hitting tab. This is a hard one to explain and what complicates it more is that it's in an AJAX responseText environment so it's hard to nail down. Any ideas on this?

Thanks for the reply. Actually the only reason I have the alert is to check to see if I have a reference to the object. So I got it to work by screwing around with the alert box, which shouldn't even be there in the first place. Without it, it doesn't work. The element is not disabled or read only.

Here is a snippet of the code. I messed with it some more and noticed that if I call the putFocusOnField() function twice, it works, but if I only call it once, regardless of location, it doesn't work. I'm baffled.

The request object suggests to me that the page uses Ajax. Since the first A stands for Asynchronous, this might be the cause of your problems. The Ajax request first needs to complete it's transactions before it can do anything else. Meanwhile the page keeps loading.. The focus won't be set, because the request object isn't finished yet or something.. This would be a logical explanation, because you said that if you do something BEFORE the setFocus() (like alert, double request setFocus, etc) it does work.. The thing you do prior to calling the method delays the script, which gives the request more time.. I have actually experienced this before, could really be the cause of your problems (even though it sounds vague here...).

Try something for starters:

Use <body onLoad=""> to call the setFocus() method, and take it out of the request object!