The javascript code below shows how to set the focus on an Input Field / Form Field / Html Element (Textbox) when the page is loaded.

<script type="text/javascript">
function setFocus()

<body onload="setFocus()">
       Name: <input type="text" id="name" size="30"><br />
       Surname: <input type="text" id="surname" size="30">


Consider also checking the following post which describes 3 techniques on setting focus on page load after postback or after submit.

Want automatic updates? Subscribe to our RSS feed or
Get Email Updates sent directly to your inbox!

Currently rated 4.3 by 9 people

  • Currently 4.333333/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5


October 19. 2009 12:44

thanks, this helped me Smile

brekeke | Reply

October 21. 2009 20:26

Social comments and analytics for this post

This post was mentioned on Twitter by bloggingdev: Setting Focus Using Javascript to an Input Field (Textbox) / Form Field / Html Element (Textbox) when the Page Loads - http://su.pr/1In5AX

uberVU - social comments | Reply

February 19. 2010 12:35


345345 | Reply

March 15. 2010 14:56

There is no such thing as a long piece of work, except one that you dare not start.

cash loans | Reply

Add comment

(Will not be displayed!)