AS3: How to set focus to an empty textfield

Applicable to: Adobe ActionScript 3, Adobe Flex, Adobe AIR

This simple operation isn’t very obvious in ActionScript 3. The TextInput element has a boolean property called focusEnabled, but this doesn’t seem to do anything (it’s probably meant for other purposes, didn’t take the time to check).

The way achieve this is two-fold:

stage.focus = yourTextField;
yourTextField.setSelection(0,0);

If your textfield contains text and you wish to place the caret to the end, you would probably go about it like so:

stage.focus = yourTextField;
yourTextField.setSelection(0, yourTextField.length);

Makes sense now you know about it, huh?

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s