Skip navigation
Currently Being Moderated

How does setFocus() method work in AS3?

Jun 27, 2012 9:00 PM

Hi everyone. I will try to keep this as simple as i can.


I'm trying to use the setFocus method for highlighting the default text in a text input field.

So bascially when user clicks on that text field - the text is highlighted and they can type over it. Currently have about 16 text fields with something akin to 'insert text here' as a prompt for our lower ICT demographic of users and it's a bit tedious to expect the user to highlight the text in each field and then type over it.


Can be performed awesomly if you tab through the text fields but I would not expect many users to use this method.


I've looked around on the net for solutions to this using as3 but I haven't found anything that has worked for me. I'd be very grateful for a suggestion or a point in the right direction....




  • Currently Being Moderated
    Jun 28, 2012 4:46 AM   in reply to FlashTapper

    If you want to have clicking a textfield end up with the text inside it selected, try the following...


    tf1.addEventListener(MouseEvent.MOUSE_UP, selectText);

    tf2.addEventListener(MouseEvent.MOUSE_UP, selectText);

    tf3.addEventListener(MouseEvent.MOUSE_UP, selectText);



    function selectText(evt:MouseEvent):void {

    Mark as:
  • Currently Being Moderated
    Jun 28, 2012 4:42 PM   in reply to FlashTapper

    You're welcome.  If you look up the setSelection method of the TextField class you might be able to understand why the -1 was used.  If you name the textfields using a numeric progression you won't need to create an array.  You can use bracket notation to target them in that loop.

    Mark as:
  • Currently Being Moderated
    Jun 28, 2012 7:24 PM   in reply to FlashTapper

    'length-1' is not likely to have a reference - it is merely performing math.  The key to understanding why -1 is used is in understanding what the arguments are in the setSelection method.

    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points