jquery.formdefaults.js 1.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. (function($) {
  2. $.fn.formDefaults = function(options) {
  3. var opts = $.extend({}, $.fn.formDefaults.defaults, options);
  4. return this.each(function() {
  5. var $this = $(this);
  6. var $form = $this.parents("form");
  7. $this
  8. .data("defaultValue", this.value)
  9. .addClass("form-default-value-processed");
  10. if (opts.inactiveColor) {
  11. $this.css("color", opts.inactiveColor);
  12. }
  13. $this
  14. .focus(function() {
  15. if (this.value == $this.data("defaultValue")) {
  16. this.value = '';
  17. this.style.color = opts.activeColor;
  18. }
  19. })
  20. .blur(function() {
  21. if (this.value == '') {
  22. this.style.color = opts.inactiveColor;
  23. this.value = $this.data("defaultValue");
  24. }
  25. });
  26. if (!$form.data("defaultValueProcessed")) {
  27. $form
  28. .data("defaultValueProcessed", true)
  29. .submit(function(e) {
  30. $(this).find(".form-default-value-processed").each(function() {
  31. var $el = $(this);
  32. if ($el.data("defaultValue") == $el.val()) {
  33. $el.val('');
  34. }
  35. });
  36. });
  37. }
  38. });
  39. };
  40. $.fn.formDefaults.defaults = {
  41. activeColor: '#000', // Color of text when form field is active
  42. inactiveColor: '' // Color of text when form field is inactive (ignored when not specified)
  43. };
  44. }(jQuery));