Skip to main content
Inspiring
February 18, 2013
Answered

Replace a keyword in string with a textBox

  • February 18, 2013
  • 1 reply
  • 914 views

I'm probably thinking about this in the wrong way. My goal is to create an input textField in a position of a key word in a textField And update the position based on that key word

Example: The planet ANSWER is the 3rd farthest away from the sun. (I want the ANSWER keyword to generate a text field in the same posion within the question).

My problem is trying to find a consistent solution to a dynamic list of questions.

My code so far:

var str:String = "hi this is ANSWER a sample.";

var theText:TextField = new TextField();

answer.text = str;

addChild(theText);

theText.y = answer.y;

//!!!! my trouble spot !!!!//

theText.x = (answer.x+theText.text.length)+str.indexOf("ANSWER");

Thanks for any help or suggestions.

This topic has been closed for replies.
Correct answer kglad

you're welcome.

and, you're correct:  the tf's need to be added to the display before their references are over-written and the input tf should use and Event.CHANGE, Event.TEXT_INPUT or TextEvent.TEXT_INPUT:

function keywordF(questionS:String,keywordS:String,x:int,y:int):void{

var nextX:int = x;

var a:Array = questionS.split(keywordS);

var tf:TextField=new TextField();

addChild(tf);

tf.x = nextX;

tf.y = y;

tf.multiline=false;

tf.text = a[0];

tf.autoSize="left";

nextX+=tf.width+5;

tf=new TextField();

addChild(tf);

tf.addEventListener(Event.CHANGE,textchangeF);  // create your textchangeF to check the user's input.

tf.multiline=false;

tf.text = keywordS;

tf.border = true;

tf.type="input";

tf.autoSize="left";

tf.x = nextX;

tf.y = y;

nextX+=tf.width+5;

tf=new TextField();

addChild(tf);

tf.x = nextX;

tf.y = y;

tf.multiline=false;

tf.text = a[2];

tf.autoSize="left";

}

1 reply

kglad
Community Expert
Community Expert
February 18, 2013

you should probably use 3 textfields.  one with the text before the keyword, the one with the keyword and one with the text after the keyword:

function keywordF(questionS:String,keywordS:String,x:int,y:int):void{

var nextX:int = x;

var a:Array = questionS.split(keywordS);

var tf:TextField=new TextField();

tf.x = nextX;

tf.y = y;

tf.multiline=false;

tf.text = a[0];

tf.autoSize="left";

nextX+=tf.width+5;

tf=new TextField();

tf.addEventListener(TextEvent.CHANGE,textchangeF);  // create your textchangeF to check the user's input.

tf.multiline=false;

tf.text = keywordS;

tf.border = true;

tf.type="input";

tf.autoSize="left";

tf.x = nextX;

tf.y = y;

nextX+=tf.width+5;

tf=new TextField();

tf.x = nextX;

tf.y = y;

tf.multiline=false;

tf.text = a[2];

tf.autoSize="left";

}

Inspiring
February 18, 2013

Awesome help! Thanks! Just a side note for anybody else using this after some debugging TextEvent will only take TEXT_INPUT or LINK. And maybe a misunderstanding on my part if I added "tf" to the display list it would only add the most recent textfield in this case the value that was split (so nothing was apprearing), I had to seperate them in order to add them to the display. Not trying to pick on your code, awesome job as always and thanks again for your help.

kglad
Community Expert
kgladCommunity ExpertCorrect answer
Community Expert
February 18, 2013

you're welcome.

and, you're correct:  the tf's need to be added to the display before their references are over-written and the input tf should use and Event.CHANGE, Event.TEXT_INPUT or TextEvent.TEXT_INPUT:

function keywordF(questionS:String,keywordS:String,x:int,y:int):void{

var nextX:int = x;

var a:Array = questionS.split(keywordS);

var tf:TextField=new TextField();

addChild(tf);

tf.x = nextX;

tf.y = y;

tf.multiline=false;

tf.text = a[0];

tf.autoSize="left";

nextX+=tf.width+5;

tf=new TextField();

addChild(tf);

tf.addEventListener(Event.CHANGE,textchangeF);  // create your textchangeF to check the user's input.

tf.multiline=false;

tf.text = keywordS;

tf.border = true;

tf.type="input";

tf.autoSize="left";

tf.x = nextX;

tf.y = y;

nextX+=tf.width+5;

tf=new TextField();

addChild(tf);

tf.x = nextX;

tf.y = y;

tf.multiline=false;

tf.text = a[2];

tf.autoSize="left";

}