MOOTOOLS JS - Clear input field onClick onFocus on click focus active


/ Published in: JavaScript
Save to your folder(s)



Copy this code and paste it in your HTML
  1. // 201004291250 - Mootools clear input on focus, return default value if no data entered
  2. // FROM: Clear text field with mootools on focus. - MooTools Users | Google Groups http://bit.ly/cHPev3
  3.  
  4. /*
  5. I've got this snippet that will clear the value on focus and if the value of
  6. the focus is '' it will put the default value again in the input. You will
  7. need the default value to be on an attribute of the input, like alt or
  8. anyother (if you don't bother about validation).
  9.  
  10. Fábio Miranda Costa
  11. Engenheiro de Computação
  12. http://meiocodigo.com
  13.  
  14. */
  15.  
  16. // FUNCTION:
  17.  
  18. Element.implement({
  19. clearFocusResetBlur: function(attr){
  20. var valueString = this.get(attr);
  21. this.addEvents({
  22. 'focus': function(){
  23. if( this.get('value') == valueString ) this.set('value','');
  24. },
  25. 'blur': function(){
  26. if( this.get('value') == "" ) this.set('value',valueString);
  27. }
  28. });
  29. }
  30. });
  31.  
  32.  
  33. // Usage
  34. // HTML:
  35. // <input id="input_id" value="some value" alt="default value" />
  36.  
  37.  
  38. // JS:
  39. $('.jform form .cleardefault').set('alt','value').clearFocusResetBlur('alt');

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.