>
 



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

<html>
<head>
<script type="text/javascript">
function setFocus()
{
     document.getElementById("name").focus();
}
</script>
</head>

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

</html>

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

Currently rated 4.3 by 9 people

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



In ASP.NET 1.x, it is not possible to programmatically set focus to a web server control without using the JavaScript's focus() function after submit (on Page Load / after Postback). You may find the details on how to set focus to web controls in ASP.NET 1.x in one of my previous articles: Set Focus After PostBack in ASP.NET 1.x - Setting Focus to an ASP.NET Control

More...

Currently rated 5.0 by 2 people

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



Management of control focus is one of the common tasks when building web applications with effective and friendly user interface. In order to set focus on a certain control such as textboxes, buttons dropdowns after postback / after submit / on Page Load in ASP.NET 1.x, we can use a dynamic javascript block that facilitates Javascript’s focus() function.

More...

Currently rated 4.0 by 4 people

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