Skip to main content
jsloop
Known Participant
November 3, 2009
Answered

Clearing the TextArea texts when I press Enter key

  • November 3, 2009
  • 4 replies
  • 1068 views

How to clear the texts that I have typed into the TextArea when I press Enter Key?

I have now added a TextArea instance called input_txt to the stage and added an component event listener to detect when I press enter key. Then an event fires up which first reads the input to a string variable.

But next I need to automatically clear the texts in the TextArea.

How to do that?

function readInput(event:Event):void
{
    input = input_txt.text;
    trace(input);

    // removing texts in the text input area-------- how?
}
input_txt.addEventListener(ComponentEvent.ENTER, readInput);

Please help.

Thank You

This topic has been closed for replies.
Correct answer

function readInput(event:Event):void
{
    input = input_txt.text;
    trace(input);

   input_txt.text = "";
}

4 replies

November 3, 2009

ComponentEvent.ENTER occurs before the line feed is added to the text, and is not cancelable, so you'll probably need to set a flag and do the clearing in an Event.CHANGE handler. Something like this:

import fl.events.ComponentEvent;
input_text.addEventListener(ComponentEvent.ENTER, readInput);
input_text.addEventListener(Event.CHANGE, changeHandler);

var clearing:Boolean=false;

function readInput(event:Event):void
{
    input = input_txt.text;
    trace(input);

    clearing=true;

}

function changeHandler(event:Event)
{
if(clearing)
{
  input_text.text="";
  clearing=false;
}
}

jsloop
jsloopAuthor
Known Participant
November 3, 2009

I have been hunting for this for a while.

The same happens for Keyboard.KEY_DOWN event too.

BTW its input_txt (I think my naming is confusing  )

Thanks man!

jsloop
jsloopAuthor
Known Participant
November 3, 2009

Cool and that works!

Thanks to all.

Ned Murphy
Legend
November 3, 2009

Try using: input_txt.text = "";

Correct answer
November 3, 2009

function readInput(event:Event):void
{
    input = input_txt.text;
    trace(input);

   input_txt.text = "";
}