Remove initial inputfield value on focus.. and place it back on blur in case of


/ Published in: JavaScript

Remove initial inputfield value on focus.. and place it back on blur in case of empty value.


Copy this code and paste it in your HTML
  1. $('input:text').focus(function(){
  2. if($(this).val() == $(this).attr('defaultValue')){
  3. $(this).val('');
  4. }
  5. });
  6.  
  7. $('input:text').blur(function(){
  8. if($(this).val() == ''){
  9. $(this).val($(this).attr('defaultValue'));
  10. }
  11. });
  12.  
  13. $('textarea').focus(function(){
  14. if($(this).val() == $(this).attr('defaultValue')){
  15. $(this).val('');
  16. }
  17. });
  18.  
  19. $('textarea').blur(function(){
  20. if($(this).val() == ''){
  21. $(this).val($(this).attr('defaultValue'));
  22. }
  23. });

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.