forms.jquery.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /**
  2. * jQuery.placeholder - Placeholder plugin for input fields
  3. * Written by Blair Mitchelmore (blair DOT mitchelmore AT gmail DOT com)
  4. * Licensed under the WTFPL (http://sam.zoy.org/wtfpl/).
  5. * Date: 2008/10/14
  6. *
  7. * @author Blair Mitchelmore
  8. * @version 1.0.1
  9. *
  10. **/
  11. new function($) {
  12. $.fn.placeholder = function(settings) {
  13. settings = settings || {};
  14. var key = settings.dataKey || "placeholderValue";
  15. var attr = settings.attr || "placeholder";
  16. var className = settings.className || "placeholder";
  17. var values = settings.values || [];
  18. var block = settings.blockSubmit || false;
  19. var blank = settings.blankSubmit || false;
  20. var submit = settings.onSubmit || false;
  21. var value = settings.value || "";
  22. var position = settings.cursor_position || 0;
  23. return this.filter(":input").each(function(index) {
  24. $.data(this, key, values[index] || $(this).attr(attr));
  25. }).each(function() {
  26. if ($.trim($(this).val()) === "")
  27. $(this).addClass(className).val($.data(this, key));
  28. }).focus(function() {
  29. if ($.trim($(this).val()) === $.data(this, key))
  30. $(this).removeClass(className).val(value)
  31. if ($.fn.setCursorPosition) {
  32. $(this).setCursorPosition(position);
  33. }
  34. }).blur(function() {
  35. if ($.trim($(this).val()) === value)
  36. $(this).addClass(className).val($.data(this, key));
  37. }).each(function(index, elem) {
  38. if (block)
  39. new function(e) {
  40. $(e.form).submit(function() {
  41. return $.trim($(e).val()) != $.data(e, key)
  42. });
  43. }(elem);
  44. else if (blank)
  45. new function(e) {
  46. $(e.form).submit(function() {
  47. if ($.trim($(e).val()) == $.data(e, key))
  48. $(e).removeClass(className).val("");
  49. return true;
  50. });
  51. }(elem);
  52. else if (submit)
  53. new function(e) { $(e.form).submit(submit); }(elem);
  54. });
  55. };
  56. }(jQuery);