AS3 – textfield setSelection()

Have you tried to set selection on atextfield after a validation procedure? Have you noticed that it doesn’t work quite right? Here’s the quick solution for that:

Normally you would use the yourTextField.setSelection(startIndex, endIndex); method, but like I said before that doesn’t really work very well.

You just need to set the focus to the textfield and then set the selection:

Example: (assuming you have a textfield called “tf”)

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

And that’s how you solve the textfield.setSelection() problem

Creative Commons License
The AS3 – textfield setSelection() by Tiago's Weblog, unless otherwise expressly stated, is licensed under a Creative Commons Attribution 2.5 Switzerland License.
Leave a comment

5 Comments.

  1. Thanks, helped me a lot!

  2. Thank you! I’ve searched high and low for this solution. Glad I found you.

  3. Kool , you are the one of the best i come across

  4. This is a great way to simulate an anchor tag but do you know if there is a way to deselect the text after the text field autoscrolls using the setSelection() function.

    • Ok just figure out once I have the index pos I can just use that as the start and end point in the setSelection() and no actual text is selected while the textfield still autoscrolls to the correct position.

Leave a Reply

Your email address will not be published.


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">