Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Regular Coder
    Join Date
    Jun 2002
    Location
    USA
    Posts
    308
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Adding focus to first form field without using body onload..

    Is this possible?

  • #2
    Regular Coder
    Join Date
    Apr 2003
    Location
    Atlanta, GA
    Posts
    487
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Of course. Use window.onload .

    Code:
    window.onload = document.forms[0].elements[0].focus;

  • #3
    Regular Coder
    Join Date
    Jun 2002
    Location
    USA
    Posts
    308
    Thanks
    0
    Thanked 0 Times in 0 Posts
    So I place that in my js file. What must I add here? (which is the 1st field)

    <input type="text" maxlength="65">

  • #4
    Regular Coder
    Join Date
    Jun 2002
    Location
    USA
    Posts
    308
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Nevermind, got it working with:

    function setFocus() {
    document.forms[0].elements[0].focus;
    }
    window.onload=setFocus;

  • #5
    Regular Coder
    Join Date
    Jul 2002
    Posts
    698
    Thanks
    0
    Thanked 0 Times in 0 Posts
    <form name=test1>
    <input type=text name="test">
    </form><script>
    document.forms['test1'].elements['test'].focus();</script>

  • #6
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,068
    Thanks
    0
    Thanked 256 Times in 252 Posts
    Originally posted by AshleyQuick
    Nevermind, got it working with:

    function setFocus() {
    document.forms[0].elements[0].focus;
    }
    window.onload=setFocus;
    function setFocus() {
    document.forms[0].elements[0].focus();
    }

    Shorter version:

    window.onload=function(){document.forms[0].elements[0].focus();}
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________


  •  

    Posting Permissions

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