Thursday, July 15, 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:


The code is quite small and easy to understand.
var defaultText="Enter your text here!"
inText.text=defaultText

inText.addEventListener(FocusEvent.FOCUS_IN, checkDefault);
function checkDefault(FocusEvent){
 if(inText.text==defaultText){
  inText.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 "inText"). When the user focuses on the text field, the default text dissapears.

No comments:

Post a Comment