File indexing completed on 2024-05-19 06:00:28
0001 /*! http://mths.be/placeholder v2.0.7 by @mathias */ 0002 ;(function(window, document, $) { 0003 0004 var isInputSupported = 'placeholder' in document.createElement('input'), 0005 isTextareaSupported = 'placeholder' in document.createElement('textarea'), 0006 prototype = $.fn, 0007 valHooks = $.valHooks, 0008 hooks, 0009 placeholder; 0010 0011 if (isInputSupported && isTextareaSupported) { 0012 0013 placeholder = prototype.placeholder = function() { 0014 return this; 0015 }; 0016 0017 placeholder.input = placeholder.textarea = true; 0018 0019 } else { 0020 0021 placeholder = prototype.placeholder = function() { 0022 var $this = this; 0023 $this 0024 .filter((isInputSupported ? 'textarea' : ':input') + '[placeholder]') 0025 .not('.placeholder') 0026 .bind({ 0027 'focus.placeholder': clearPlaceholder, 0028 'blur.placeholder': setPlaceholder 0029 }) 0030 .data('placeholder-enabled', true) 0031 .trigger('blur.placeholder'); 0032 return $this; 0033 }; 0034 0035 placeholder.input = isInputSupported; 0036 placeholder.textarea = isTextareaSupported; 0037 0038 hooks = { 0039 'get': function(element) { 0040 var $element = $(element); 0041 return $element.data('placeholder-enabled') && $element.hasClass('placeholder') ? '' : element.value; 0042 }, 0043 'set': function(element, value) { 0044 var $element = $(element); 0045 if (!$element.data('placeholder-enabled')) { 0046 return element.value = value; 0047 } 0048 if (value == '') { 0049 element.value = value; 0050 // Issue #56: Setting the placeholder causes problems if the element continues to have focus. 0051 if (element != document.activeElement) { 0052 // We can't use `triggerHandler` here because of dummy text/password inputs :( 0053 setPlaceholder.call(element); 0054 } 0055 } else if ($element.hasClass('placeholder')) { 0056 clearPlaceholder.call(element, true, value) || (element.value = value); 0057 } else { 0058 element.value = value; 0059 } 0060 // `set` can not return `undefined`; see http://jsapi.info/jquery/1.7.1/val#L2363 0061 return $element; 0062 } 0063 }; 0064 0065 isInputSupported || (valHooks.input = hooks); 0066 isTextareaSupported || (valHooks.textarea = hooks); 0067 0068 $(function() { 0069 // Look for forms 0070 $(document).delegate('form', 'submit.placeholder', function() { 0071 // Clear the placeholder values so they don't get submitted 0072 var $inputs = $('.placeholder', this).each(clearPlaceholder); 0073 setTimeout(function() { 0074 $inputs.each(setPlaceholder); 0075 }, 10); 0076 }); 0077 }); 0078 0079 // Clear placeholder values upon page reload 0080 $(window).bind('beforeunload.placeholder', function() { 0081 $('.placeholder').each(function() { 0082 this.value = ''; 0083 }); 0084 }); 0085 0086 } 0087 0088 function args(elem) { 0089 // Return an object of element attributes 0090 var newAttrs = {}, 0091 rinlinejQuery = /^jQuery\d+$/; 0092 $.each(elem.attributes, function(i, attr) { 0093 if (attr.specified && !rinlinejQuery.test(attr.name)) { 0094 newAttrs[attr.name] = attr.value; 0095 } 0096 }); 0097 return newAttrs; 0098 } 0099 0100 function clearPlaceholder(event, value) { 0101 var input = this, 0102 $input = $(input); 0103 if (input.value == $input.attr('placeholder') && $input.hasClass('placeholder')) { 0104 if ($input.data('placeholder-password')) { 0105 $input = $input.hide().next().show().attr('id', $input.removeAttr('id').data('placeholder-id')); 0106 // If `clearPlaceholder` was called from `$.valHooks.input.set` 0107 if (event === true) { 0108 return $input[0].value = value; 0109 } 0110 $input.focus(); 0111 } else { 0112 input.value = ''; 0113 $input.removeClass('placeholder'); 0114 input == document.activeElement && input.select(); 0115 } 0116 } 0117 } 0118 0119 function setPlaceholder() { 0120 var $replacement, 0121 input = this, 0122 $input = $(input), 0123 $origInput = $input, 0124 id = this.id; 0125 if (input.value == '') { 0126 if (input.type == 'password') { 0127 if (!$input.data('placeholder-textinput')) { 0128 try { 0129 $replacement = $input.clone().attr({ 'type': 'text' }); 0130 } catch(e) { 0131 $replacement = $('<input>').attr($.extend(args(this), { 'type': 'text' })); 0132 } 0133 $replacement 0134 .removeAttr('name') 0135 .data({ 0136 'placeholder-password': true, 0137 'placeholder-id': id 0138 }) 0139 .bind('focus.placeholder', clearPlaceholder); 0140 $input 0141 .data({ 0142 'placeholder-textinput': $replacement, 0143 'placeholder-id': id 0144 }) 0145 .before($replacement); 0146 } 0147 $input = $input.removeAttr('id').hide().prev().attr('id', id).show(); 0148 // Note: `$input[0] != input` now! 0149 } 0150 $input.addClass('placeholder'); 0151 $input[0].value = $input.attr('placeholder'); 0152 } else { 0153 $input.removeClass('placeholder'); 0154 } 0155 } 0156 0157 }(this, document, jQuery));