To put focus in the first field (form)

Discussion in 'JavaScript' started by piropeator, Apr 15, 2009.

  1. #1
    I have this code and I want to put focus on the field name, after that put the focus in the last name field and finally the focus will be on OK button.

    <html>
    <head>
    </head>
    <body>
    <form name="form" action="process.php" method="post" >
    <p>Name:
    <input type="text" name="name"/>
    <br/>
    Lastname:
    <input type="text" name="lastname"/>
    <br/>
    <input type="button" value="Return" onClick="javascript:document.location.href='index.php'"/>
    <input name="submit" type="submit" value="Ok"/>
    </p>
    </form>
    </body>
    </html>

    How to do that?
     
    piropeator, Apr 15, 2009 IP
  2. jqueryHowto

    jqueryHowto Guest

    Messages:
    10
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Using jQuery that would be:

    $('input :first').focus();

    or if u wanna be more specific then:

    $('input[type="text"] :first').focus(); // or
    $('input[name="name"]').focus();
     
    jqueryHowto, Apr 15, 2009 IP
  3. piropeator

    piropeator Well-Known Member

    Messages:
    194
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    121
    #3
    Can you tell me how do I do that ? I don't know JavaScript very well.
    Thanks.
     
    piropeator, May 23, 2010 IP
  4. Lord

    Lord Active Member

    Messages:
    134
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #4
    and maybe he doesn't have jQuery ...

    just modify the <body> tag to this
    <body onload="document.form.name.focus();">
    Code (markup):
    and it should work
     
    Lord, May 27, 2010 IP