View Full Version : How to automatically set focus on a textfield that is not associated with a form

12-17-2011, 10:42 PM
I know you can use this to set focus in a textfield:


the problem is this only works for a textfield in a form. I'd like to do the same for a textfield that is not connected with a form.

it's not connected to a form because it runs Ajax.

12-17-2011, 10:52 PM
That's no reason why it can't be in a form. But it doesn't have to be anyway. There are many ways to access an element as an object and give it focus. Only certain elements can receive focus though. But you're in luck, a text input is one of them. Or do you mean a textarea? That's one too, so either way you're covered. The easiest way would be, if doesn't already have one, to give it a unique id. Say it's a text input:

<input id="mytextfield" type="text">

Then you can give it focus with:


12-17-2011, 10:55 PM
Awesome thank you! I didn't that would work!