jquery.hint.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. /**
  2. * @author Remy Sharp
  3. * @url http://remysharp.com/2007/01/25/jquery-tutorial-text-box-hints/
  4. */
  5. (function ($) {
  6. $.fn.hint = function (blurClass) {
  7. if (!blurClass) {
  8. blurClass = 'blur';
  9. }
  10. return this.each(function () {
  11. // get jQuery version of 'this'
  12. var $input = $(this),
  13. // capture the rest of the variable to allow for reuse
  14. title = $input.attr('title'),
  15. $form = $(this.form),
  16. $win = $(window);
  17. function remove() {
  18. if ($input.val() === title && $input.hasClass(blurClass)) {
  19. $input.val('').removeClass(blurClass);
  20. }
  21. }
  22. // only apply logic if the element has the attribute
  23. if (title) {
  24. // on blur, set value to title attr if text is blank
  25. $input.blur(function () {
  26. if (this.value === '') {
  27. $input.val(title).addClass(blurClass);
  28. }
  29. }).focus(remove).blur(); // now change all inputs to title
  30. // clear the pre-defined text when form is submitted
  31. $form.submit(remove);
  32. $win.unload(remove); // handles Firefox's autocomplete
  33. }
  34. });
  35. };
  36. })(jQuery);