Posted By

misteroneill on 05/27/09


Tagged

form input jquery Value


Versions (?)

Who likes this?

6 people have marked this snippet as a favorite

jseltzer
marvin_speakman
evanheisler
szoftverhiba
LeGaS
petrykowski


jQuery Clear Default Input Values


 / Published in: jQuery
 

This function loops through a result set of inputs and for each result, it clears the default value onfocus and restores the default if the value is empty onblur.

  1. (function($){
  2. $.fn.clearDefault = function(){
  3. return this.each(function(){
  4. var default_value = $(this).val();
  5. $(this).focus(function(){
  6. if ($(this).val() == default_value) $(this).val("");
  7. });
  8. $(this).blur(function(){
  9. if ($(this).val() == "") $(this).val(default_value);
  10. });
  11. });
  12. };
  13. })(jQuery);
  14.  
  15. // Usage: $('input.clear-default').clearDefault();

Report this snippet  

You need to login to post a comment.