How to set focus on first textbox of the page

Below I have given the complete sample code where the cursor will be placed on first textbox when the page loads.


<html xmlns="">
<head id="Head1" runat="server">
    <title>Ashish's Blog</title>
    <script type="text/javascript" src=""></script>
    <script type="text/javascript">
        $(function () {
            $('form input:text:enabled:first').focus();
    <form id="form1" runat="server">
    <asp:TextBox ID="TextBox1" runat="server" />
    <asp:TextBox ID="TextBox2" runat="server" />
    <asp:TextBox ID="TextBox3" runat="server" />
    <asp:TextBox ID="TextBox4" runat="server" />


If you first textbox is disabled

$(document).ready(function() {

If you first textbox is not visible

$(document).ready(function() {


2 responses to “How to set focus on first textbox of the page”

  1. Hi,

    Your code works well but it gets failed when my first text box was not visible. It was set to invisible using css.


Leave a Reply to Virendra Dugar