From bcd5ce49cb6eca0dbde3680e0e9ce830d8553121 Mon Sep 17 00:00:00 2001 From: Mathias Bynens Date: Tue, 5 Oct 2010 16:49:11 +0200 Subject: [PATCH] =?UTF-8?q?Switched=20to=20namespaced=20events.=20Rewrote/?= =?UTF-8?q?optimized=20some=20other=20stuff=20too.=20Much=20=E2=99=A5=20to?= =?UTF-8?q?=20Sean=20Koole=20and=20temp01!?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- jquery.placeholder.js | 145 +++++++++++++++++++------------------- jquery.placeholder.min.js | 4 +- 2 files changed, 75 insertions(+), 74 deletions(-) diff --git a/jquery.placeholder.js b/jquery.placeholder.js index 813e3dc..7a2033b 100644 --- a/jquery.placeholder.js +++ b/jquery.placeholder.js @@ -1,78 +1,79 @@ /*! - * HTML5 Placeholder jQuery Plugin v1.4 + * HTML5 Placeholder jQuery Plugin v1.5 * @link http://github.com/mathiasbynens/Placeholder-jQuery-Plugin * @author Mathias Bynens */ ;(function($) { - $.fn.placeholder = function() { - // Quit if there’s support for HTML5 placeholder - if (this[0] && 'placeholder' in document.createElement('input')) { - // Allow chaining - return this; - } - function args($elem) { - // Get attributes string from outerHTML - var html = $('
').append($elem.clone()).html().replace(/<(\w+)\s+(.*)>/, '$2'), - attr, - attrs = {}; - while ((attr = html.match(/\s*([\w-]+)=("[^"]*"|'[^']*'|\w+)/))) { - // Assign attribute to dictionary, but remove quotes first - attrs[attr[1]] = attr[2].replace(/^(["'])(.*?)\1$/, '$2'); - html = html.replace(attr[0], ''); - } - return attrs; - } - function onFocus() { - var $input = $(this); - if ($input.val() === $input.attr('placeholder') && $input.hasClass('placeholder')) { - if ($input.data('placeholder-password')) { - $input.next().show().focus().end().remove(); - } else { - $input.val('').removeClass('placeholder'); - } - } - } - // Made this a function, because we actually need it on two different occasions: - // 1) Once when the DOM is loaded; - // 2) Once every time the focusout() is triggered. - function setPlaceholder($elem) { - var $replacement; - if ($elem.val() === '' || $elem.val() === $elem.attr('placeholder')) { - if ($elem.is(':password')) { - try { - $replacement = $elem.clone().attr({ type: 'text' }); - } catch(e) { - $replacement = $('', $.extend(args($elem), { type: 'text' })); - } - $replacement.data('placeholder-password', true).focus(onFocus); - $elem.hide().before($replacement); - $elem = $replacement; - } - $elem.addClass('placeholder').val($elem.attr('placeholder')); - } else { - $elem.removeClass('placeholder'); - } - } - // Look for forms with inputs and/or textareas with a placeholder attribute in them - $('form:has([placeholder])').submit(function() { - // Clear the placeholder values so they don’t get submitted - $('.placeholder', this).val(''); - }); - // Clear placeholder values upon page reload - $(window).unload(function() { - $('.placeholder').val(''); - }); - // Yes, .each() — in case .placeholder() is called on several elements, which is very likely, e.g. $('input').placeholder(); - return this.each(function() { - var $input = $(this); - // Quit if the current element doesn’t have a placeholder attribute, or is not an input/textarea at all - if (!$input.attr('placeholder') || !$input.is(':input')) { - return; - } - setPlaceholder($input); - $input.focus(onFocus).blur(function() { - setPlaceholder($input); - }); - }); - }; + + function args($elem) { + // Get attributes string from outerHTML + var html = $('
').append($elem.clone()).html().replace(/<(\w+)\s+(.*)>/, '$2'), + attr, + attrs = {}; + while ((attr = html.match(/\s*([\w\-]+)=("[^"]*"|'[^']*'|\w+)/))) { + // Assign attribute to dictionary, but remove quotes first + attrs[attr[1]] = attr[2].replace(/^(["'])(.*?)\1$/, '$2'); + html = html.replace(attr[0], ''); + } + return attrs; + } + + function onFocus() { + var $input = $(this); + if ($input.val() === $input.attr('placeholder') && $input.hasClass('placeholder')) { + if ($input.data('placeholder-password')) { + $input.next().show().focus().end().remove(); + } else { + $input.val('').removeClass('placeholder'); + } + } + } + + // Made this a function, because we actually need it on two different occasions: + // 1) Once when the DOM is loaded; + // 2) Once every time the focusout() is triggered. + function setPlaceholder(elem) { + var $replacement, + $elem = $(this); + if ($elem.val() === '' || $elem.val() === $elem.attr('placeholder')) { + if ($elem.is(':password')) { + try { + $replacement = $elem.clone().attr({ type: 'text' }); + } catch(e) { + $replacement = $('', $.extend(args($elem), { type: 'text' })); + } + $replacement.data('placeholder-password', true).bind('focus.placeholder', onFocus); + $elem.hide().before($replacement); + $elem = $replacement; + } + $elem.addClass('placeholder').val($elem.attr('placeholder')); + } else { + $elem.removeClass('placeholder'); + } + } + + // Look for forms + $('form').bind('submit.placeholder', function() { + // Clear the placeholder values so they don’t get submitted + $('.placeholder', this).val(''); + }); + + // Clear placeholder values upon page reload + $(window).bind('unload.placeholder', function() { + $('.placeholder').val(''); + }); + + if ('placeholder' in document.createElement('input')) { + $.fn.placeholder = function() { + return this; + }; + } else { + $.fn.placeholder = function() { + return this.filter(':input[placeholder]').bind({ + 'focus.placeholder': onFocus, + 'blur.placeholder': setPlaceholder + }).trigger('blur.placeholder').end(); + }; + } + })(jQuery); \ No newline at end of file diff --git a/jquery.placeholder.min.js b/jquery.placeholder.min.js index 0f31631..033f5a5 100644 --- a/jquery.placeholder.min.js +++ b/jquery.placeholder.min.js @@ -1,6 +1,6 @@ /*! - * HTML5 Placeholder jQuery Plugin v1.4 + * HTML5 Placeholder jQuery Plugin v1.5 * @link http://github.com/mathiasbynens/Placeholder-jQuery-Plugin * @author Mathias Bynens */ -(function(a){a.fn.placeholder=function(){if(this[0]&&'placeholder' in document.createElement('input')){return this}function b(f){var h=a('
').append(f.clone()).html().replace(/<(\w+)\s+(.*)>/,'$2'),e,g={};while((e=h.match(/\s*([\w-]+)=("[^"]*"|'[^']*'|\w+)/))){g[e[1]]=e[2].replace(/^(["'])(.*?)\1$/,'$2');h=h.replace(e[0],'')}return g}function c(){var e=a(this);if(e.val()===e.attr('placeholder')&&e.hasClass('placeholder')){if(e.data('placeholder-password')){e.next().show().focus().end().remove()}else{e.val('').removeClass('placeholder')}}}function d(f){var h;if(f.val()===''||f.val()===f.attr('placeholder')){if(f.is(':password')){try{h=f.clone().attr({type:'text'})}catch(g){h=a('',a.extend(b(f),{type:'text'}))}h.data('placeholder-password',true).focus(c);f.hide().before(h);f=h}f.addClass('placeholder').val(f.attr('placeholder'))}else{f.removeClass('placeholder')}}a('form:has([placeholder])').submit(function(){a('.placeholder',this).val('')});a(window).unload(function(){a('.placeholder').val('')});return this.each(function(){var e=a(this);if(!e.attr('placeholder')||!e.is(':input')){return}d(e);e.focus(c).blur(function(){d(e)})})}})(jQuery); \ No newline at end of file +(function(d){function a(f){var h=d('
').append(f.clone()).html().replace(/<(\w+)\s+(.*)>/,'$2'),e,g={};while((e=h.match(/\s*([\w\-]+)=("[^"]*"|'[^']*'|\w+)/))){g[e[1]]=e[2].replace(/^(["'])(.*?)\1$/,'$2');h=h.replace(e[0],'')}return g}function b(){var e=d(this);if(e.val()===e.attr('placeholder')&&e.hasClass('placeholder')){if(e.data('placeholder-password')){e.next().show().focus().end().remove()}else{e.val('').removeClass('placeholder')}}}function c(g){var i,f=d(this);if(f.val()===''||f.val()===f.attr('placeholder')){if(f.is(':password')){try{i=f.clone().attr({type:'text'})}catch(h){i=d('',d.extend(a(f),{type:'text'}))}i.data('placeholder-password',true).bind('focus.placeholder',b);f.hide().before(i);f=i}f.addClass('placeholder').val(f.attr('placeholder'))}else{f.removeClass('placeholder')}}d('form').bind('submit.placeholder',function(){d('.placeholder',this).val('')});d(window).bind('unload.placeholder',function(){d('.placeholder').val('')});if('placeholder' in document.createElement('input')){d.fn.placeholder=function(){return this}}else{d.fn.placeholder=function(){return this.filter(':input[placeholder]').bind({'focus.placeholder':b,'blur.placeholder':c}).trigger('blur.placeholder').end()}}})(jQuery); \ No newline at end of file