Guest User

Untitled

a guest
Jul 21st, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.70 KB | None | 0 0
  1. /**
  2. * Set or Fetch a HTML5 Placeholder
  3. * without backwards compatibility for older browsers
  4. * @author Benjamin Arthur Lupton
  5. * @license Public Domain
  6. */
  7. $.fn.placeholder = $.fn.placeholder||function(placeholder) {
  8. // Prepare
  9. var $input = $(this);
  10.  
  11. // Check
  12. if ( typeof placeholder === 'undefined' ) {
  13. return $input.attr('placeholder')||$input.attr('title');
  14. }
  15.  
  16. // Config
  17. var
  18. config = {
  19. hasClass: 'sparkle-hint-has',
  20. hintedClass: 'sparkle-hint-hinted',
  21. };
  22.  
  23. // Events
  24. var events = {
  25. focusEvent: function(){
  26. var $input = $(this);
  27. var tip = $input.placeholder();
  28. var val = $input.val();
  29. // Handle
  30. if (tip === val) {
  31. $input.val('').removeClass(config.hintedClass);
  32. }
  33. // Done
  34. return true;
  35. },
  36. blurEvent: function(){
  37. var $input = $(this);
  38. var tip = $input.placeholder();
  39. var val = $input.val();
  40. // Handle
  41. if (tip === val || !val) {
  42. $input.val('').addClass(config.hintedClass).val(tip);
  43. }
  44. // Done
  45. return true;
  46. },
  47. submitEvent: function(){
  48. $(this).find('input.'+config.hasClass).trigger('focus');
  49. }
  50. };
  51.  
  52. // Apply
  53. if ( typeof Modernizr !== 'undefined' && Modernizr.input.placeholder ) {
  54. // We Support HTML5 Hinting
  55. // Set the placeholder as the title if the placeholder does not exist
  56. // We could use a filter selector, however we believe this should be faster - not benchmarked though
  57. $input.attr('title',placeholder).attr('placeholder',placeholder);
  58. }
  59. else {
  60. // We Support Javascript Hinting
  61. $input.attr('title',placeholder).attr('placeholder',placeholder);
  62. $input.once('focus',events.focusEvent).once('blur',events.blurEvent).trigger('blur');
  63. $input.parents('form').once('submit',events.submitEvent);
  64. }
  65.  
  66. // Chain
  67. return $input;
  68. };
Add Comment
Please, Sign In to add comment