From ce625cdcb34388c5b38aa74199efccabf71bcfc1 Mon Sep 17 00:00:00 2001 From: Mathias Bynens Date: Thu, 20 Jan 2011 00:07:05 +0100 Subject: [PATCH] Allow the user to enter values that are equal to the placeholder value. Fixes #1. --- jquery.placeholder.js | 13 +++++++++---- jquery.placeholder.min.js | 4 ++-- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/jquery.placeholder.js b/jquery.placeholder.js index bb8bbd7..d1befa5 100644 --- a/jquery.placeholder.js +++ b/jquery.placeholder.js @@ -1,5 +1,5 @@ /*! - * HTML5 Placeholder jQuery Plugin v1.7 + * HTML5 Placeholder jQuery Plugin v1.8 * @link http://github.com/mathiasbynens/Placeholder-jQuery-Plugin * @author Mathias Bynens */ @@ -45,8 +45,10 @@ function setPlaceholder(elem) { var $replacement, - $input = $(this); - if ($input.val() === '' || $input.val() === $input.attr('placeholder')) { + $input = $(this), + $origInput = $input, + isInitialized = $input.data('placeholder-init'); + if ($input.val() === '' || (!isInitialized && $input.val() === $input.attr('placeholder'))) { if ($input.is(':password')) { if (!$input.data('placeholder-textinput')) { try { @@ -68,6 +70,9 @@ } else { $input.removeClass('placeholder'); } + if (!isInitialized) { + $origInput.data('placeholder-init', true); + } } $(function() { @@ -86,4 +91,4 @@ $('.placeholder').val(''); }); -})(jQuery); \ No newline at end of file +}(jQuery)); \ No newline at end of file diff --git a/jquery.placeholder.min.js b/jquery.placeholder.min.js index 18ad056..f15f7a2 100644 --- a/jquery.placeholder.min.js +++ b/jquery.placeholder.min.js @@ -1,6 +1,6 @@ /*! - * HTML5 Placeholder jQuery Plugin v1.7 + * HTML5 Placeholder jQuery Plugin v1.8 * @link http://github.com/mathiasbynens/Placeholder-jQuery-Plugin * @author Mathias Bynens */ -(function(f,z){var e=z in document.createElement('input'),a=z in document.createElement('textarea');if(e&&a){f.fn.placeholder=function(){return this}}else{f.fn.placeholder=function(){return this.filter((e?'textarea':':input')+'['+z+']').bind('focus.'+z,b).bind('blur.'+z,d).trigger('blur.'+z).end()}}function c(h){var g={},i=/^jQuery\d+$/;f.each(h.attributes,function(k,j){if(j.specified&&!i.test(j.name)){g[j.name]=j.value}});return g}function b(){var g=f(this);if(g.val()===g.attr(z)&&g.hasClass(z)){if(g.data(z+'-password')){g.hide().next().show().focus()}else{g.val('').removeClass(z)}}}function d(g){var j,i=f(this);if(i.val()===''||i.val()===i.attr(z)){if(i.is(':password')){if(!i.data(z+'-textinput')){try{j=i.clone().attr({type:'text'})}catch(h){j=f('').attr(f.extend(c(i[0]),{type:'text'}))}j.removeAttr('name').data(z+'-password',true).bind('focus.'+z,b);i.data(z+'-textinput',j).before(j)}i=i.hide().prev().show()}i.addClass(z).val(i.attr(z))}else{i.removeClass(z)}}f(function(){f('form').bind('submit.'+z,function(){var g=f('.'+z,this).each(b);setTimeout(function(){g.each(d)},10)})});f(window).bind('unload.'+z,function(){f('.'+z).val('')})})(jQuery,'placeholder'); \ No newline at end of file +(function(f){var e='placeholder' in document.createElement('input'),a='placeholder' in document.createElement('textarea');if(e&&a){f.fn.placeholder=function(){return this}}else{f.fn.placeholder=function(){return this.filter((e?'textarea':':input')+'[placeholder]').bind('focus.placeholder',b).bind('blur.placeholder',d).trigger('blur.placeholder').end()}}function c(h){var g={},i=/^jQuery\d+$/;f.each(h.attributes,function(k,j){if(j.specified&&!i.test(j.name)){g[j.name]=j.value}});return g}function b(){var g=f(this);if(g.val()===g.attr('placeholder')&&g.hasClass('placeholder')){if(g.data('placeholder-password')){g.hide().next().show().focus()}else{g.val('').removeClass('placeholder')}}}function d(i){var l,k=f(this),h=k,g=k.data('placeholder-init');if(k.val()===''||(!g&&k.val()===k.attr('placeholder'))){if(k.is(':password')){if(!k.data('placeholder-textinput')){try{l=k.clone().attr({type:'text'})}catch(j){l=f('').attr(f.extend(c(k[0]),{type:'text'}))}l.removeAttr('name').data('placeholder-password',true).bind('focus.placeholder',b);k.data('placeholder-textinput',l).before(l)}k=k.hide().prev().show()}k.addClass('placeholder').val(k.attr('placeholder'))}else{k.removeClass('placeholder')}if(!g){h.data('placeholder-init',true)}}f(function(){f('form').bind('submit.placeholder',function(){var g=f('.placeholder',this).each(b);setTimeout(function(){g.each(d)},10)})});f(window).bind('unload.placeholder',function(){f('.placeholder').val('')})}(jQuery)); \ No newline at end of file