Flash/Flex AS3 - How to Select / Highlight All Text in an Input Text Field On Click

This took forever to google for some reason...

Issue:

You want an input textbox to highlight everything in it when the user clicks it (on focus). Problem is, when the user clicks the text box places the carat, thus un-highlighting the text. I could be wrong, but the second problem seems to be that if you use the FocusEvent.FOCUS_IN event setting selection doesn't work, so you're forced to use MouseEvent.CLICK event... but if you highlight the entire box every time the user clicks, the user can't select any text because it always just highlights the entire box... also, once the box has been highlighted once, you want the user to be able to place the carat without re-selecting the entire box again.

Solution:

What you want to do is check if the user is highlighting or if the box was already highlighted since last focus, and if not, stop flash player from putting the carat at the location clicked with event.preventDefault() function, and then set selection from beginning to end. Event.preventDefault() is a sneaky little function you call right on the event object in your event handler, and it prevents whatever the default actions would be for that event (in this case, clear selected text and place carat)

as3 code example:

// assuming you have a textbox input called _myTextInputBox

import flash.events.FocusEvent;
import flash.events.MouseEvent;
import flash.text.TextField;

_myTextInputBox.addEventListener(MouseEvent.CLICK, onClickTextBox);
_myTextInputBox.addEventListener(FocusEvent.FOCUS_OUT, onTextBoxLoseFocus);

var _highlightTextOnClick:Boolean = true;

function onClickTextBox(e:Event):void {

  var didUserHighlight:Boolean = Boolean(e.target.selectionBeginIndex != e.target.selectionEndIndex);
			
  if ( _highlightTextOnClick && !didUserHighlight)  {
    e.preventDefault();
    e.target.setSelection(0, e.target.text.length);
    _highlightTextOnClick = false;
  }

}

function onTextBoxLoseFocus(e:FocusEvent):void 
{
  _highlightTextOnClick = true;
}

5 comments (Add your own)

1. osiris wrote:
this would be better if it would apply to any textbox, but how do you apply dynamic properties to a text field object without having to extend it?

in other words, the textbox has a variable attached to it called something like TextBox(textboxRef).highlightMeOnFocus == true

then this solution could be applied to any textbox / re-used, but you will get the error from flash player about not being able to apply a dynamic property to a TextField class instance...

any way around this?

September 23, 2009 @ 1:08 AM

2. sean wrote:
...could use this code in a class that extends TextField called something like

TextFieldAutoSelect

September 26, 2009 @ 3:32 AM

3. Fred wrote:
Feels great when finding a ready-to-use solution for a simple problem that somehow AS3 made difficult. Great job.

Minor mistake on the 'onTextBoxLoseFocus' function. It is defined as 'onTitleBoxLoseFocus' later on the code. Pick one. :)

November 10, 2009 @ 2:41 PM

4. sean wrote:
Ah yes, thanks fred- I rewrote this one out by hand in the blog WYSIWYG editor, probably should have compiled it first!

error fixed :)

November 13, 2009 @ 5:15 PM

5. Ray Ban Sunglasses wrote:
First of all, do not hurry when buying Coach Bags , Footwear is the matter of fashion and style. It is advisable to feel free to walk in any physical shop nearby or visit the online UGG Boots on sale shop, and make sure that you clear all your doubts. You can even refer to magazines and fashion blogs to know what type of ghd Hair Straighteners are in fashion. Normally, girls and women prefer to buy shoes according to the seasons. For instance, women choose strappy MBT Shoes for summer and ankle Chanel Jewelry for the winter. Moreover, colour, heels, style, and price also play an important role in Ray Ban Sunglasses shopping.

April 7, 2010 @ 12:35 AM

Add a New Comment

Enter the code you see below:
code
 

Comment Guidelines: No HTML is allowed. Off-topic or inappropriate comments will be edited or deleted. Thanks.