Thursday, February 21, 2013

Auto Focus the ASP.NET TextBox using jQuery


.aspx Markup:

<form id="form1" runat="server">
<div class="smallDiv">
<h2>Auto Focus the First TextBox that has Enabled=true</h2>
<asp:TextBox ID="tb1" runat="server" Enabled="false" />
<asp:TextBox ID="tb2" runat="server" Enabled="false" />
<asp:TextBox ID="tb3" runat="server" />
<asp:TextBox ID="tb4" runat="server" Text="Some Text"/>
<br />
Tip: The First two textboxes have been disabled.
You cannot enter text in them.
</div>
</form>

Jquery Script:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript">
$(function() {
$("input:text:disabled").css("background-color","gray");
$("input:text:enabled:first").focus();
});
</script>

Cursor will be focused on the first text box where Enabled=true.




No comments:
Write comments
Recommended Posts × +