Posted By

sidneydekoning on 06/04/09


Tagged

form default Value focus TextField as3


Versions (?)

Who likes this?

4 people have marked this snippet as a favorite

Pingpongbal
donaldleegraham
JohnGeorge
Priestd09


Fill in fields with default values and empty field on focus


 / Published in: ActionScript 3
 

  1. /*
  2. Create two input field on the stage with name_txt and msg_txt as a name
  3.  
  4. You can also set the text format onFocus.
  5. So when nothing is filled in, it is grey and when you start typing it is black
  6.  
  7. */
  8.  
  9. var defaultNameEntry:String = "fill in your name";
  10. var defaultMessageEntry:String = "fill in your message";
  11.  
  12. name_txt.tabIndex = 0;
  13. msg_txt.tabIndex = 1;
  14.  
  15. name_txt.text = defaultNameEntry;
  16. msg_txt.text = defaultMessageEntry;
  17.  
  18. setupEventlisteners();
  19.  
  20.  
  21. function setupEventlisteners():void {
  22. name_txt.addEventListener(FocusEvent.FOCUS_IN, focusInHandler);
  23. name_txt.addEventListener(FocusEvent.FOCUS_OUT, focusOutHandler);
  24.  
  25. msg_txt.addEventListener(FocusEvent.FOCUS_IN, focusInHandler);
  26. msg_txt.addEventListener(FocusEvent.FOCUS_OUT, focusOutHandler);
  27. }
  28.  
  29. function focusInHandler(e:FocusEvent):void {
  30. if (e.currentTarget.name == "name_txt") {
  31. if (e.currentTarget.text == defaultNameEntry) {
  32. e.currentTarget.text = "";
  33. }
  34. } else {
  35. if (e.currentTarget.text == defaultMessageEntry) {
  36. e.currentTarget.text = "";
  37. }
  38. }
  39. }
  40.  
  41. function focusOutHandler(e:FocusEvent):void {
  42. if (e.currentTarget.name == "name_txt") {
  43. if (e.currentTarget.text == "") {
  44. e.currentTarget.text = defaultNameEntry;
  45. }
  46. } else {
  47. if (e.currentTarget.text == "") {
  48. e.currentTarget.text = defaultMessageEntry;
  49. }
  50. }
  51. }

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: viaria on July 8, 2009

thanks, it solve my problem, there is too many wrong information about this on internet, real solution appear 2.or 3. pages on search, it is socks,

Posted By: sidneydekoning on November 3, 2009

You're welcome :)

Posted By: JohnGeorge on November 19, 2010

Dude i cant thank you enough for this I even reg'd an acc to say this. Am a flash jedi but with AS3 i feel like the universe has just released "The Force 3.0" and I just end up cutting my baws off with my lightsaber while trying to open a door!!

Epic win mate thank you.

Posted By: nandugan on July 28, 2011

Hi,

I have added the code like this, but i am receiving an error message.

1083: Syntax error: else is unexpected.

==============

var defaultNameEntry:String = "Name"; var defaultEmailEntry:String = "Email"; var defaultMessageEntry:String = "Enquiry";

nameField.tabIndex = 0; emailField.tabIndex = 1; enquiryField.tabIndex = 2;

nameField.text = defaultNameEntry; emailField.text = defaultEmailEntry; enquiryField.text = defaultMessageEntry;

setupEventlisteners();

function setupEventlisteners():void { nameField.addEventListener(FocusEvent.FOCUSIN, focusInHandler); nameField.addEventListener(FocusEvent.FOCUSOUT, focusOutHandler);

emailField.addEventListener(FocusEvent.FOCUS_IN, focusInHandler);
emailField.addEventListener(FocusEvent.FOCUS_OUT, focusOutHandler);

enquiryField.addEventListener(FocusEvent.FOCUS_IN, focusInHandler);
enquiryField.addEventListener(FocusEvent.FOCUS_OUT, focusOutHandler);

}

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

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

How to solve this. Kindly please help.

Thanks

You need to login to post a comment.