Home > html, javascript > Autofocus on a textfield after page load [HTML]

Autofocus on a textfield after page load [HTML]

Problem

After page load, you want to put the focus on a textfield. For instance you have a form and you want to focus on the first field thus users can type immediately. See for instance the Google search page.

Solution

I tried several variations and this is what worked for me:

<body onload="document.login_form.email.focus()">

Here I suppose to have the following form:

<form name="login_form" action="process.php" method="POST">
    <input type="text" name="email" />
    ...
</form>

This tip is from here.

Update (20151007)
As it is pointed out here, you can use the autofocus attribute on HTML5 compatible browsers:

<input name="myinput" value="whatever" autofocus />
Categories: html, javascript Tags:
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: