• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

How does setFocus() method work in AS3?

Explorer ,
Jun 27, 2012 Jun 27, 2012

Copy link to clipboard

Copied

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....

Thanks

TOPICS
ActionScript

Views

3.1K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , Jun 28, 2012 Jun 28, 2012

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);

etc...

function selectText(evt:MouseEvent):void {
    evt.currentTarget.setSelection(0,evt.currentTarget.length-1);
}

Votes

Translate

Translate
LEGEND ,
Jun 28, 2012 Jun 28, 2012

Copy link to clipboard

Copied

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);

etc...

function selectText(evt:MouseEvent):void {
    evt.currentTarget.setSelection(0,evt.currentTarget.length-1);
}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jun 28, 2012 Jun 28, 2012

Copy link to clipboard

Copied

Thanks Ned...did the trick

Although I'm not sure what you mean about the 'lenght-1' but - I jsut got rid of the -1 as it was not selecting all of my text.

Also I think I'm going to set up an array and run a for while loop to assign the functions to all of my textfields - there are about 19 of them!

There is now way I'm going to set up all the even listeners for all of them individually.

Cheers.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jun 28, 2012 Jun 28, 2012

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jun 28, 2012 Jun 28, 2012

Copy link to clipboard

Copied

That's true but I decided to go with using array and naming them. Easier for me to refer to them later with name and not number.

May get confused with numbering system. I will look up the setSelection method as you indicated. So far removing the '-1' from the function has worked for me.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jun 28, 2012 Jun 28, 2012

Copy link to clipboard

Copied

I looked around on the net and on the flash help file but could not find reference to the 'length-1' reference.

Could you provide a link about this at all? just very curious.

Thanks

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jun 28, 2012 Jun 28, 2012

Copy link to clipboard

Copied

LATEST

'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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines