How to select all text in textbox when it gets focus
Posted by Tihomir Ivanov on 13 November 2008 09:40
Rating: 7.00
Sometimes it's usefull when your someone clicks in your textbox, whole text inside it to become selected. We use it in 'Link to Us' textbox. Follows example how you can do it:
in aspx page:
<asp:TextBox ID="_linkToUsTB" runat="server" TextMode="MultiLine" Width="150px" Height="100px"></asp:TextBox>
in code-behind:
protected void Page_Load(object sender, EventArgs e)
{
. . .
_linkToUsTB.Text = "<a href=\"http://www.aspnetsource.com\" title=\"asp.net directory\">" +
+ "<img style=\"border-width: 0px; height: 70px; width: 116px;\"src=\"http://www.aspnetsource.com/images/logo.jpg\" alt=\"asp.net directory\" />"
+ "</a>";
_linkToUsTB.Attributes.Add("onClick", "javascript:this.form." + _linkToUsTB.ClientID + ".focus(); this.form." + _linkToUsTB.ClientID + ".select();");
. . .
}
The example works on IE (6, 7), FireFox, Opera, Safari and Chrome.
That's all. I hope it'll be usefull to someone too :)
Gotta love that Firefox winning formula. See what Opera does...then do exactly that. For a moment I thought Firefox was innovating with its "awesome bar" and even read several books on this problem ( http://www.ebook-search-queen.com/ ) But upon inspection, I found that typing "awesome bar" in opera displays...this page.
use onFocus rather than onClick !
yeah it worked! thanks
good guest page.
thank you.
good guest page.
thank you.