Results 1 to 3 of 3

Thread: How to automatically set focus on a textfield that is not associated with a form

  1. #1
    Join Date
    Jan 2009
    Posts
    82
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default How to automatically set focus on a textfield that is not associated with a form

    I know you can use this to set focus in a textfield:

    onload="document.formwhatever.textfieldwhatever.focus();"


    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.

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    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:

    Code:
    <input id="mytextfield" type="text">
    Then you can give it focus with:

    Code:
    document.getElementById('mytextfield').focus();
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. The Following User Says Thank You to jscheuer1 For This Useful Post:

    monaya (12-17-2011)

  4. #3
    Join Date
    Jan 2009
    Posts
    82
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Thumbs up

    Awesome thank you! I didn't that would work!

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •