SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Zealot
    Join Date
    Dec 2003
    Location
    Ireland
    Posts
    116
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Setting focus to SELECT

    Hi all, how do you set focus to a SELECT box

    I have..

    document.dForm.Day_of_Birth2.options[2].selected = true;
    document.dForm.Date_Of_Birth.focus();

    ...my first line sets the SELECT to the correct option, but my second line doesn't seem to work. This a a fairly long form so I really need to 'jump' to the SELECT to show the user which SELECT is incorrect.

    Thanks

  2. #2
    SitePoint Wizard chris_fuel's Avatar
    Join Date
    May 2006
    Location
    Ventura, CA
    Posts
    2,751
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    Your second line's select variable name looks nothing like the first. I'm somewhat confused.

  3. #3
    SitePoint Wizard dreamscape's Avatar
    Join Date
    Aug 2005
    Posts
    1,080
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    document.dForm.Day_of_Birth2.focus();

  4. #4
    SitePoint Zealot
    Join Date
    Dec 2003
    Location
    Ireland
    Posts
    116
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry guys, that was just a typo..

    it should have been

    document.dForm.Day_of_Birth2.options[2].selected = true;
    document.dForm.Date_Of_Birth2.focus();

    Why doesn't it jump to SELECT box, like it would with an INPUT.

    Regards
    Melt

  5. #5
    SitePoint Wizard dreamscape's Avatar
    Join Date
    Aug 2005
    Posts
    1,080
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you still have it typed wrong. If you type if correctly, it works just fine for me.

    *edit add*
    Though you may want to note that you can't focus on a select object in some browsers (Safari 2 will not for example); but it does work in Firefox that I know of.

    When you say "jump" to the select, do you mean you want the page to scroll to it? That's not what focus() does. If you want to scroll to the element, you'll have to get its position (there are several javascript "getPosition" functions floating around the net), and scroll the page to it.
    Last edited by dreamscape; Jul 1, 2006 at 10:21.


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
  •