Skip to main content
Participant
December 14, 2016
Answered

Find a text field name using a variable

  • December 14, 2016
  • 2 replies
  • 253 views

Hello, I know this may be very basic, so I apologize in advance for that, but I just can´t figure it out after a couple of hours of Google it, so if anyone can help me, that would be great! Thanks in advance!

I have three dynamic text fields in my stage, called txt1, txt2 and txt3.

I want to put some data in them, but I want to find them using a variable:

var i: int = 1;

while (i < 4)) {

  var textTemp : String = i.toString();

  textTemp = "txt" + textTemp;

  trace(textTemp); //  So far, so good: in the Console, I get txt1, txt2 and txt3

  i++;

//  Now, lets say I want to set each text field to any text, it doesn´t really matter. So I try this:

textTemp.text = "This is a test"; //  This didn´t work

TextField[textTemp].text1 = "This is a test"; //  Neither this

}

Thanks again, I´m really lost here!

This topic has been closed for replies.
Correct answer kglad

use array notation to coerce strings into objects:

var i: int = 1;

while (i < 4)) {

var textTemp : String = i.toString();

textTemp = "txt" + textTemp;

trace(textTemp); // So far, so good: in the Console, I get txt1, txt2 and txt3

i++;

// Now, lets say I want to set each text field to any text, it doesn´t really matter. So I try this:

this[textTemp].text = "This is a test";

}

2 replies

Participant
December 14, 2016

Work like a charm, thank you, kglad, I was driving myself crazy with this!

kglad
Community Expert
Community Expert
December 14, 2016

you're welcome.

kglad
Community Expert
kgladCommunity ExpertCorrect answer
Community Expert
December 14, 2016

use array notation to coerce strings into objects:

var i: int = 1;

while (i < 4)) {

var textTemp : String = i.toString();

textTemp = "txt" + textTemp;

trace(textTemp); // So far, so good: in the Console, I get txt1, txt2 and txt3

i++;

// Now, lets say I want to set each text field to any text, it doesn´t really matter. So I try this:

this[textTemp].text = "This is a test";

}