Return to Snippet

Revision: 14437
at June 4, 2009 05:49 by sidneydekoning


Initial Code
/*
Create two input field on the stage with name_txt and msg_txt as a name

You can also set the text format onFocus.
So when nothing is filled in, it is grey and when you start typing it is black

*/

var defaultNameEntry:String = "fill in your name";
var defaultMessageEntry:String = "fill in your message";

name_txt.tabIndex = 0;
msg_txt.tabIndex = 1;

name_txt.text = defaultNameEntry;
msg_txt.text = defaultMessageEntry;

setupEventlisteners();


function setupEventlisteners():void {
	name_txt.addEventListener(FocusEvent.FOCUS_IN, focusInHandler);
	name_txt.addEventListener(FocusEvent.FOCUS_OUT, focusOutHandler);

	msg_txt.addEventListener(FocusEvent.FOCUS_IN, focusInHandler);
	msg_txt.addEventListener(FocusEvent.FOCUS_OUT, focusOutHandler);
}

function focusInHandler(e:FocusEvent):void {
	if (e.currentTarget.name == "name_txt") {
		if (e.currentTarget.text == defaultNameEntry) {
			e.currentTarget.text = "";
		}
	} else {
		if (e.currentTarget.text == defaultMessageEntry) {
			e.currentTarget.text = "";
		}
	}
}

function focusOutHandler(e:FocusEvent):void {
	if (e.currentTarget.name == "name_txt") {
		if (e.currentTarget.text == "") {
			e.currentTarget.text = defaultNameEntry;
		}
	} else {
		if (e.currentTarget.text == "") {
			e.currentTarget.text = defaultMessageEntry;
		}
	}
}

Initial URL


Initial Description


Initial Title
Fill in fields with default values and empty field on focus

Initial Tags
form

Initial Language
ActionScript 3