TextEvent.TEXT_INPUT does not work as expected

83 Views Asked by At

Hello there i had another problem but this one is weird and does not work as expected
I had a text input component i add a function to it, when a user type a text it start to search in an array, the problem is when i type the first letter the output is blank and when i type the second letter the function reads only the first letter here is the code
workArray is the object that stores workTitle and workCost

work.addEventListener(TextEvent.TEXT_INPUT, inputInWorkBox);
function inputInWorkBox(TextEvent) {
    trace("Function worked: "+work.text.toLowerCase());
    todayWorkList.removeAll();
        var typedWork = work.text.toLowerCase();
        var fullWork:String = "";
    for (i = 0; i <= workNumber; i++) {
        fullWork = (workArray[i].workTitle).toLowerCase();
        if (fullWork.search(typedWork) != -1) {
            todayWorkList.visible = true;
            todayWorkList.addItem({
                label: (workArray[i].workTitle),
                data: workArray[i]
            });
        }
    }
}

here is the output


Function worked: // i typed a
Function worked: a // i typed ab
Function worked: ab // i typed abc
Function worked: abc // i typed abcd

BTW: i don't want to use CHANGE Event because the data in the text input changed by other functions to display some data i want it to call the function only when a user type inside that specific text input
thanx in advance

1

There are 1 best solutions below

3
On

Here's the quote from the CHANGE event:

Dispatched after a control value is modified, unlike the textInput event, which is dispatched before the value is modified.

That means that TextEvent.TEXT_INPUT is dispatched before the text in the TextField is actually changed, which totally agrees with the results you get (although it is not explicitly mentioned in the TEXT_INPUT chapter itself) AND works EXACTLY as expected.

Then, the documentation states that TEXT_INPUT event has a text property which contains the new content of the TextField.

Thus, your code should be something like

work.addEventListener(TextEvent.TEXT_INPUT, onInput);

// This is a wrong thing to do: inputInWorkBox(TextEvent)
// Do not declare entities with the names similar to class names.
// Use some simple variable names instead.
function onInput(e:TextEvent):void
{
    // Extract the new text (which is not passed to the TextField yet).
    var aText:String = e.text.toLowerCase();
    
    trace("The new text is:", aText);
    
    // Proceed processing aText variable from here on.