jQuery to select all text in Asp.Net and HTML TextBox on click or focus

Introduction: In this article I will show with an example to select all text in Asp.Net textbox, multiline textbox, HTML input and textarea control on single click or focus using jQuery. 

jQuery to select all text in Asp.Net and HTML TextBox on click or focus

In previous articles i explained How to move items from one listbox to another in asp.net using jQuery and Jquery to check uncheck all checkboxes in repeater in asp.net and Jquery tovalidate file type and size before uploading through fileupload control and Jquery to show hide password characters in textbox on checkbox check uncheck and Jquery to convert textbox text to uppercase or lowercase while typing


Description: While working on Asp.Net project I got the requirement to select all text in textbox on single click. I have used simple jquery’s select and focus method for this purpose.

Implementation: Let’s create a web page for demonstration purpose:

HTML source:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
    <script type="text/javascript">
function selectAllText(sender)
        {
            $(sender).focus().select();
        }      
 </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>                
<asp:TextBox ID="TextBox1" runat="server" Width="250px" Text="Dummy text in Asp.Net Textbox" onclick="selectAllText(this)"></asp:TextBox><br />
        <asp:TextBox ID="TextBox2" runat="server" Text="This is dummy text in Asp.Net Multiline Textbox" TextMode="MultiLine" Rows="3" Columns="33" onclick="selectAllText(this)"></asp:TextBox><br /><br />
        <input type="text" style="width:250px;" value="Dummy text in HTML input control" onclick="selectAllText(this)" /><br />
        <textarea rows="3" cols="33" onclick="selectAllText(this)">This is dummy text in HTML textarea</textarea>  
    </div>
    </form>
</body>
</html>

Now over to you:
A blog is nothing without reader's feedback and comments. So please provide your valuable feedback so that i can make this blog better and If you like my work; you can appreciate by leaving your comments, hitting Facebook like button, following on Google+, Twitter, Linkedin and Pinterest, stumbling my posts on stumble upon and subscribing for receiving free updates directly to your inbox . Stay tuned and stay connected for more technical updates. 
Previous
Next Post »

If you have any question about any post, Feel free to ask.You can simply drop a comment below post or contact via Contact Us form. Your feedback and suggestions will be highly appreciated. Also try to leave comments from your account not from the anonymous account so that i can respond to you easily..