Fill in fields with default values and empty field on focus


/ Published in: ActionScript 3
Save to your folder(s)



Copy this code and paste it in your HTML
  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

You need to login to post a comment.