jquery.placeholdr.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /**
  2. * Placeholdr: a jQuery Plugin
  3. * @author: Jonathan Stahlhacke
  4. * @url: http://www.porkcullis.com/lab/placeholdr/
  5. * @documentation: http://www.porkcullis.com/lab/placeholdr/
  6. * @published: 2009-09-02
  7. * @license: MIT
  8. *
  9. */
  10. if(typeof jQuery != 'undefined') {
  11. jQuery(function($) {
  12. $.fn.extend({
  13. placeholdr: function(options) {
  14. var settings = $.extend({}, $.fn.placeholdr.defaults, options);
  15. return this.filter(":text").each(
  16. function() {
  17. if($.fn.jquery < '1.2.6') {return;}
  18. var $t = $(this);
  19. var o = $.metadata ? $.extend({}, settings, $t.metadata()) : settings;
  20. $t.addClass(settings.className);
  21. var phtext = $t.val();
  22. if (settings.placeholderText != null)
  23. {
  24. phtext = settings.placeholderText;
  25. if ($t.val() == "")
  26. {
  27. $t.val(phtext);
  28. }
  29. else
  30. {
  31. $t.removeClass(settings.className);
  32. }
  33. }
  34. $t.data("placeholdr_text", phtext);
  35. $t.focus(function(){
  36. if ($(this).hasClass(settings.className))
  37. {
  38. $(this).removeClass(settings.className);
  39. $(this).val("");
  40. }
  41. });
  42. $t.blur(function(){
  43. if ($(this).val() == "")
  44. {
  45. $(this).addClass(settings.className);
  46. $(this).val($(this).data("placeholdr_text"));
  47. }
  48. });
  49. if (settings.clearOnSubmit)
  50. {
  51. $t.parents("form").submit(function(){
  52. if ($t.hasClass(settings.className))
  53. {
  54. $t.val("");
  55. }
  56. });
  57. }
  58. }
  59. );
  60. }
  61. });
  62. /**
  63. * Set your Plugin Defaults Here…
  64. */
  65. $.fn.placeholdr.defaults = {
  66. className: 'placeholder',
  67. placeholderText: null,
  68. clearOnSubmit: true
  69. };
  70. });
  71. }