Start the cursor in the search box

Discussion in 'JavaScript' started by jarvi, May 16, 2004.

  1. #1
    I have a very basic php page that really just displays the standard menu items for the site plus a search box using a form and submit button. How do I get it so that the cursor is in the search box and the user can just start typing, rather than having to click in the box first?

    Google does it if you can't understand what I am saying.
     
    jarvi, May 16, 2004 IP
  2. nlopes

    nlopes Guest

    Messages:
    103
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    This is just a simple javascript code.

    In <head> add this:
    <script>
    <!--
    function focus(){document.FORM_NAME.INPUT_NAME.focus();}
    // -->
    </script>

    And then in <body>:
    <body onLoad=focus()>
     
    nlopes, May 16, 2004 IP
    digitalpoint likes this.
  3. jarvi

    jarvi Well-Known Member

    Messages:
    127
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    103
    #3
    Thanks, mate. Knew it wold be something simple.
     
    jarvi, May 17, 2004 IP
  4. webnp05

    webnp05 Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Does this work with Safari on a mac?
     
    webnp05, Jul 14, 2005 IP
  5. J.D.

    J.D. Peon

    Messages:
    1,198
    Likes Received:
    65
    Best Answers:
    0
    Trophy Points:
    0
    #5
    This syntax is not standard - only elements in HTML collections can be accessed using this notation (i.e. using id's as JS identifiers). This syntax will work in all DOM-compliant browsers:

    document.getElementById("input-id").focus();

    input-id must be the value of the ID attribute of the input element, not name.

    J.D.
     
    J.D., Jul 15, 2005 IP