jQuery function to clear inputs


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

Simply run the function when the document has loaded and specify setDefault to true.


Copy this code and paste it in your HTML
  1. function initInput(setDefault) {
  2.  
  3. var inputValues = {};
  4.  
  5. if (setDefault == null) setDefault = false;
  6.  
  7. $('input:text, input:password').each(function() {
  8. if (setDefault) inputValues['' + $(this).attr('id') + ''] = $(this).val();
  9.  
  10. $(this).focus(function() {
  11. if ($(this).val() == inputValues[$(this).attr('id')]) $(this).val('');
  12. });
  13.  
  14. $(this).blur(function() {
  15. if ($(this).val() == '') $(this).val(inputValues[$(this).attr('id')]);
  16. });
  17.  
  18. });
  19.  
  20. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.