This article will show you how you can select all or highlight the text
present in a textbox or in a multiline textbox on focus or on click inside the
textbox using jQuery in asp.net.
Some of my previous arils are as follows: jQuery
Dialog Box Open On Button Click In Asp.Net, jQuery
Datepicker Calendar With Restrict Date Range in Asp.net, jQuery
Datepicker Calendar Open On Image Button Click in Asp.net, Autocomplete
Textbox Using jQuery In Asp.Net and C#.Net, jQuery
Datepicker Calendar With Multiple Months in Asp.net, Disable
jQuery UI DatePicker Calendar On Button Click In Asp.Net, jQuery
DatePicker Calendar With Dropdown Month and Year in Asp.Net, How
to Use jQuery Calender In MVC3 | jQuery UI Datepicker Calender Control In
Asp.Net Mvc Application.
So for this article first we will create a new asp.net application
and add the jQuery library reference in our asp.net page.
<script language="javascript"
src="http://code.jquery.com/jquery-1.10.2.js"></script>
|
Now please check the below code for selecting all the text
present in the textbox.
$("#<%=Control Id %>").click(function
() { $(this).select(); });
|
So in above code we have raised event when mouse enter
inside the textbox and user click on textbox. So here is the complete code.
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="SelectTextInTextBox.aspx.cs"
Inherits="ProjectDemo_Asp.et.SelectTextInTextBox"
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>jQuery
Code to Select All Text in TextBox on Click in Asp.Net</title>
<script language="javascript" src="http://code.jquery.com/jquery-1.10.2.js"></script>
<script language="javascript">
$(document).ready(function () {
/*This
is for TextBox*/
$("#<%=TextBox1.ClientID
%>").click(function () { $(this).select();
});
/*This
is for multiline textbox*/
$("#<%=TextBox2.ClientID
%>").click(function () { $(this).select();
});
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
TextBox:
<asp:TextBox ID="TextBox1" runat="server" Width="180px" Text="This is a demo text from asp.net"></asp:TextBox><br />
TextArea:<asp:TextBox ID="TextBox2" runat="server" Height="106px" TextMode="MultiLine"
Text="This is a demo text from asp.net"></asp:TextBox>
</div>
</form>
</body>
</html>
|
So just run the code and check the output.
0 comments:
Please let me know your view