`

Thursday, September 16, 2010

Clear text field when focused on

If you are making an input text field, it's often needed to add hints, such as "Enter your name here" and "Enter your email", etc. If you are a good programmer, you will make this default text dissapear when the field is clicked, like in this example:

Click here to view Preview

The code is quite small and easy to understand.

var startingText = "Enter your text here!";
name_txt.text = startingText;
name_txt.addEventListener(FocusEvent.FOCUS_IN, checkTheText);

function checkTheText(event:FocusEvent):void{
if(name_txt.text == startingText){
name_txt.text="";
}
}


First we enter the default text we want to show, then apply it
to our text field (in this example - an input field with
instance "name_txt"). When the user focuses on the text field,
the default text dissapears.

Download .zip

No comments: