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?