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

Find a text field name using a variable

New Here ,
Dec 13, 2016 Dec 13, 2016

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!

TOPICS
ActionScript
214
Translate
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

Community Expert , Dec 13, 2016 Dec 13, 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";

}

Translate
Community Expert ,
Dec 13, 2016 Dec 13, 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";

}

Translate
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
New Here ,
Dec 14, 2016 Dec 14, 2016

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

Translate
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
Community Expert ,
Dec 14, 2016 Dec 14, 2016
LATEST

you're welcome.

Translate
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