jQuery auto focus.

In this post we will see how to set focus automatically on input control. This is done using .focus() function in jQuery.

jQuery auto focus example

<script type="text/javascript">
   $(function () {
        $("#fName").focus();
   });
</script>

.focus()function is simple and we can call it on input control on which we need to set focus.

Below is simple full example of .focus() in jQuery

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript">
$(function () {
$("#fName").focus();
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<input type="text" id="fName"/>
<input type="text" id="lName"/>
</div>
</form>
</body>
</html>