I'm trying to handle a focus event on a TextField so I can select all the text when focusing (tab or click). Seems like I'm doing something wrong here ?
txtTextField.addEventListener(FocusEvent.FOCUS_IN, handleFocusIn);
function handleFocusIn() {
//select all text here
}
I needed the same thing, to select the contents of a textfield when it receives focus.
I tried:
A) Simply selecting after a FocusEvent. This doesn't seem to work (my guess is that FocusEvents are fired before the mouse click is being processed, which in turn will undo the selection).
B) Selecting on every mouse click. This works, but this is very annoying for a user who wants to select only a part of the text later, since this attempt will always result in -all- the content being selected.
The following workaround seems to work though:
myTextField.addEventListener(MouseEvent.CLICK, selectAllOnce);
function selectAllOnce(e:MouseEvent) {
e.target.removeEventListener(MouseEvent.CLICK, selectAllOnce);
e.target.addEventListener(FocusEvent.FOCUS_OUT, addSelectListener);
selectAll(e);
}
function addSelectListener(e:FocusEvent) {
e.target.addEventListener(MouseEvent.CLICK, selectAllOnce);
e.target.removeEventListener(FocusEvent.FOCUS_OUT, addSelectListener);
}
function selectAll(e:Event) {
e.target.setSelection(0, e.target.getLineLength(0));
}
Hope that helps. I personally think it would be most logical if adobe simply added an option for this for the TextField object.
Your handleFocusIn should have the signature
function handleFocusIn(event:FocusEvent) // or just Event
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With