Set or clear input values on load, focus & blur with jQuery


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



Copy this code and paste it in your HTML
  1. $("body input").each(function(){
  2. if ($(this).val() == '') {
  3. $(this).val($(this).attr('title'));
  4. $(this).css('color','#ddd');
  5. } else if ($(this).val() == $(this).attr('title')) {
  6. $(this).css('color','#ddd');
  7. } else {
  8. $(this).css('color','#000');
  9. }
  10. });
  11.  
  12. // Set the value to the title attribute if blank or clear the value on focus if set to the title attribute
  13.  
  14. $("input").focus(function() {
  15. if ($(this).val() == $(this).attr('title')) {
  16. $(this).val('');
  17. $(this).css("color","#000");
  18. }
  19. }).blur(function() {
  20. if ($(this).val() == '') {
  21. $(this).val($(this).attr('title'));
  22. $(this).css("color","#ddd");
  23. } else if ($(this).val() == $(this).attr('title')) {
  24. $(this).css("color","#ddd");
  25. }
  26.  
  27. });
  28.  

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.