Skip to main content
Known Participant
August 23, 2008
Question

Why does this "Cannot create property text on String" error happen?

  • August 23, 2008
  • 2 replies
  • 1476 views
I have three text boxes(textBox1, textBox2, textBox3) on stage that I want a single message to be passed onto each text box but can't seem to get it to work. The code below keeps getting this error message: "Cannot create property text on String." How do I make "textBox" not a string.

var textBoxes:Array = new Array();

var winnerText:String = "You Won";

var randomNum:Number = Math.ceil(Math.random() * 3);
var i:int;
var n:int;
for (i = 1; i <= 3; i++)
{
textBoxes.push("textBox" + i);
n = i-1;
textBoxes.text = winnerText;
}
This topic has been closed for replies.

2 replies

kglad
Community Expert
Community Expert
August 25, 2008
there's no TextField() function. that's a class name and should be used with the "new" constructor as shown in andrei's code, if you want to create a textfield instance.
Inspiring
August 23, 2008
You need a TextField instance. For example:

textBoxes.push(new TextField());
textBoxes.text = winnerText;
yombullAuthor
Known Participant
August 23, 2008
Thanks for your help. I've tried what you said but when I do a "trace(textBoxes.text)", I get "undefined" in the output box. How do I set the text field's instance name?

var textBoxes:Array = new Array();
var winnerText:String = "You Won";

var randomNum:Number = Math.ceil(Math.random() * 3);
var i:int;
var n:int;
for (i = 1; i <= 3; i++)
{
textBoxes.push(new TextField());
n = i-1;
textBoxes.text = winnerText;
}
trace(textBoxes.text);
trace(randomNum);
Inspiring
August 24, 2008
textBoxes is an array and as such it doesn't have "text" property - thus it is undefined. But each element of the array does have text property. So, in order to get the text from a particular element of the array you have to write: textBoxes .text where i is the index of a text field in the array.