Skip to main content
Participant
August 2, 2011
Question

Input Text Field Search Box

  • August 2, 2011
  • 2 replies
  • 580 views

I am new to actionscript and trying to figure out how to create a search box.  I've been experimenting by placing a dynamic classic text field on the stage with some text already in it. I've also placed an input text field on the stage to use as a search box.

I try to type in a single word to search for in the search box but it won't return any index value. I only get an index returned if the input text exactly matches the entire string in the dynamic text field. How do I get the search box to return the index of only an input substring?

Here's my code:

import flash.text.*;

import flash.events.*;

var index:int = -1;

submit_btn.addEventListener(MouseEvent.CLICK, textSearch);

function searchText():void {

trace("Button Clicked");

while ((index = dynamicText.text.indexOf(inputText.text, index + 1)) != -1)

{

trace(inputText.text + " at index " + index);

}

}

function textSearch(e:MouseEvent):void {

searchText();

}

This topic has been closed for replies.

2 replies

relaxatraja
Inspiring
August 2, 2011

"=" typo Error might be "=="

Participant
August 2, 2011

Ok, I got it sorted out. I thought that the text property of a TextField object was a string. I had to convert the TextField object's text to a string and hold that in a new variable before I could use it with indexOf():

var str:String = TextField.text.toString()

I don't know if any of that made sense. I realize that working with text is not the place for a newbie to start but I like a good challenge anyway. Thanks for all your responses.

kglad
Community Expert
Community Expert
August 2, 2011

you can use the indexOf() property of strings:

if(stringtosearch.indexOf("searchstring")>-1){

//found

}