Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Set or Fetch a HTML5 Placeholder
- * without backwards compatibility for older browsers
- * @author Benjamin Arthur Lupton
- * @license Public Domain
- */
- $.fn.placeholder = $.fn.placeholder||function(placeholder) {
- // Prepare
- var $input = $(this);
- // Check
- if ( typeof placeholder === 'undefined' ) {
- return $input.attr('placeholder')||$input.attr('title');
- }
- // Config
- var
- config = {
- hasClass: 'sparkle-hint-has',
- hintedClass: 'sparkle-hint-hinted',
- };
- // Events
- var events = {
- focusEvent: function(){
- var $input = $(this);
- var tip = $input.placeholder();
- var val = $input.val();
- // Handle
- if (tip === val) {
- $input.val('').removeClass(config.hintedClass);
- }
- // Done
- return true;
- },
- blurEvent: function(){
- var $input = $(this);
- var tip = $input.placeholder();
- var val = $input.val();
- // Handle
- if (tip === val || !val) {
- $input.val('').addClass(config.hintedClass).val(tip);
- }
- // Done
- return true;
- },
- submitEvent: function(){
- $(this).find('input.'+config.hasClass).trigger('focus');
- }
- };
- // Apply
- if ( typeof Modernizr !== 'undefined' && Modernizr.input.placeholder ) {
- // We Support HTML5 Hinting
- // Set the placeholder as the title if the placeholder does not exist
- // We could use a filter selector, however we believe this should be faster - not benchmarked though
- $input.attr('title',placeholder).attr('placeholder',placeholder);
- }
- else {
- // We Support Javascript Hinting
- $input.attr('title',placeholder).attr('placeholder',placeholder);
- $input.once('focus',events.focusEvent).once('blur',events.blurEvent).trigger('blur');
- $input.parents('form').once('submit',events.submitEvent);
- }
- // Chain
- return $input;
- };
Add Comment
Please, Sign In to add comment