From 0a1f1cab68c875efda837597e220b598b4124526 Mon Sep 17 00:00:00 2001 From: Mathias Bynens Date: Fri, 2 Jul 2010 12:57:49 +0200 Subject: [PATCH] =?UTF-8?q?Don=E2=80=99t=20focus=20empty=20inputs=20on=20s?= =?UTF-8?q?ubmit=20to=20allow=20for=20optional=20fields=20with=20placehold?= =?UTF-8?q?ers.=20Fixes=20issue=20#2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- jquery.placeholder.js | 20 ++++++++++---------- jquery.placeholder.min.js | 4 ++-- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/jquery.placeholder.js b/jquery.placeholder.js index dad08c9..a2c2602 100644 --- a/jquery.placeholder.js +++ b/jquery.placeholder.js @@ -1,5 +1,5 @@ /*! - * HTML5 Placeholder jQuery Plugin + * HTML5 Placeholder jQuery Plugin v1.0 * @link http://github.com/mathiasbynens/Placeholder-jQuery-Plugin * @author Mathias Bynens */ @@ -20,17 +20,17 @@ $elem.removeClass('placeholder'); }; }; - // Submit handler on all forms containing input[placeholder] - // Needs to be via .click() - $('form:has([placeholder]) :submit').click(function() { - // if ($(this).has('.placeholder') never returns false here — WTF? - if ($(this.form).find('.placeholder').length) { - $(this.form).find('.placeholder:first').val('').focus().removeClass('placeholder'); - return false; - }; + // This selector could be shortened to form:has([placeholder]) but that would be less efficient + $('form:has(input[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() { + return this.each(function() { var $input = $(this); // Quit if the current element is a password input, or not an input at all if ($input.is(':password') || !$input.is(':input')) { diff --git a/jquery.placeholder.min.js b/jquery.placeholder.min.js index b5e51da..a0de164 100644 --- a/jquery.placeholder.min.js +++ b/jquery.placeholder.min.js @@ -1,6 +1,6 @@ /*! - * HTML5 Placeholder jQuery Plugin + * HTML5 Placeholder jQuery Plugin v1.0 * @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(c){if(c.val()===''||c.val()===c.attr('placeholder')){c.addClass('placeholder').val(c.attr('placeholder'))}else{c.removeClass('placeholder')}}a('form:has([placeholder]) :submit').click(function(){if(a(this.form).find('.placeholder').length){a(this.form).find('.placeholder:first').val('').focus().removeClass('placeholder');return !1}});return a(this).each(function(){var c=a(this);if(c.is(':password')||!c.is(':input')){return}b(c);c.focusin(function(){if(c.val()===c.attr('placeholder')){c.val('').removeClass('placeholder')}}).focusout(function(){b(c)})})}})(jQuery); \ No newline at end of file +(function(a){a.fn.placeholder=function(){if(this[0]&&'placeholder' in document.createElement('input')){return this}function b(c){if(c.val()===''||c.val()===c.attr('placeholder')){c.addClass('placeholder').val(c.attr('placeholder'))}else{c.removeClass('placeholder')}}a('form:has(input[placeholder])').submit(function(){a('.placeholder',this).val('')});a(window).unload(function(){a('.placeholder').val('')});return this.each(function(){var c=a(this);if(c.is(':password')||!c.is(':input')){return}b(c);c.focusin(function(){if(c.val()===c.attr('placeholder')){c.val('').removeClass('placeholder')}}).focusout(function(){b(c)})})}})(jQuery); \ No newline at end of file