Posted By

phil7085 on 06/26/10


Tagged

input jquery Value load focus Blur


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

Priestd09


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


 / Published in: JavaScript
 

  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  

You need to login to post a comment.