2

i finished it with the help of Andreas. i also add some more code to get the default text value of targeted text field. that helps me to set the text of targeted field back to default when i focus out. thanks "Andreas".

import flash.events.FocusEvent;
import flash.text.TextField;

input1.addEventListener(FocusEvent.FOCUS_IN,inHand);
input1.addEventListener(FocusEvent.FOCUS_OUT,outHand);
//add all the other text input references like I did below...
input2.addEventListener(FocusEvent.FOCUS_IN, inHand);
input2.addEventListener(FocusEvent.FOCUS_OUT, outHand);

var def1:String = input1.text;
var def2:String = input2.text;

function inHand(evt:FocusEvent):void
{
var textField:TextField = TextField(evt.target);
textField.text = "";
}

function outHand(evt:FocusEvent):void
{
var textField:TextField = TextField(evt.target);
if(textField.text == "")
{
    switch(textField.name)
    {
        case "input1":
            textField.text = def1;
        break;

        case "input2":
            textField.text = def2;
        break;

        default:
        break;
    }

  }
}
albhee
  • 23
  • 6
  • I think you are going to have to loop trough all your children and see if they are there and are textfield and apply the event to them. – putvande Aug 15 '13 at 19:07

1 Answers1

0

Yes, by simply referring to the displayObject provided in the FocusEvent, we can obtain a reference to the hovered object. This allows you to create generic references to the object being hovered, ultimately allowing you to place it on as many TextFields as you want.

import flash.events.FocusEvent;
import flash.text.TextField;

input1.addEventListener(FocusEvent.FOCUS_IN,inHand);
input1.addEventListener(FocusEvent.FOCUS_OUT,outHand);
//add all the other text input references like I did below...
input2.addEventListener(FocusEvent.FOCUS_IN, inHand);
input2.addEventListener(FocusEvent.FOCUS_OUT, outHand);

var def1:String = "Your text value here";

function inHand(evt:FocusEvent):void
{
    var textField:TextField = TextField(evt.target);
    textField.text = "";
}

function outHand(evt:FocusEvent):void
{
    var textField:TextField = TextField(evt.target);
    if(textField.text == "")
    {
        textField.text = def1;
    }
}
Andreas
  • 417
  • 2
  • 5
  • Thanks for your replay. but i have faced a problem. it works inversely. in output window an error message comes as below: * * *TypeError: Error #1009: Cannot access a property or method of a null object reference. at Untitled_1_fla::MainTimeline/inHand() TypeError: Error #1009: Cannot access a property or method of a null object reference. at Untitled_1_fla::MainTimeline/outHand() – albhee Aug 15 '13 at 18:16
  • @albhee I have corrected the code to fix the errors you are encountering. I mistakenly used relatedObject instead of target. – Andreas Aug 15 '13 at 19:22