From 1ee4b18b2d39652988235ae906e330d68586ad31 Mon Sep 17 00:00:00 2001 From: Mathias Bynens Date: Wed, 3 Feb 2010 22:19:40 +0100 Subject: [PATCH] Bug fix --- jquery.placeholder.js | 24 ++++++++---------------- jquery.placeholder.min.js | 2 +- 2 files changed, 9 insertions(+), 17 deletions(-) diff --git a/jquery.placeholder.js b/jquery.placeholder.js index 0722a94..99d5ca1 100644 --- a/jquery.placeholder.js +++ b/jquery.placeholder.js @@ -20,33 +20,25 @@ $elem.removeClass('placeholder'); }; }; - function preventSubmit($form) { - if ($form.has('.placeholder')) { - $form.find('.placeholder:first').val('').focus().removeClass('placeholder'); + // Submit handler on all forms containing input[placeholder] + $('form:has([placeholder])').click(function() { + // if ($(this).has('.placeholder') never returns false here — WTF? + if ($(this).find('.placeholder').length) { + $(this).find('.placeholder:first').val('').focus().removeClass('placeholder'); return false; }; - }; + }); // 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 is a password input, or not an input at all if ($input.is(':password') || !$input.is(':input')) { return; - } + }; setPlaceholder($input); - // Cancel both the submit event of this form and the click event of the submit button of this form - // I found this to be necessary when using the jQuery Validation Plugin - // Even preventDefault() failed — AMIDOINITRITE?! - $(this.form).submit(function() { - // preventDefault(); doesn’t seem to work here - preventSubmit($(this)); - }).find('input[type=submit]').click(function() { - // preventDefault(); doesn’t seem to work here - preventSubmit(this.form); - }); $input.focusin(function() { if ($input.val() === $input.attr('placeholder')) { - $input.removeClass('placeholder').val(''); + $input.val('').removeClass('placeholder'); }; }).focusout(function() { setPlaceholder($input); diff --git a/jquery.placeholder.min.js b/jquery.placeholder.min.js index 0f0b04c..e55ffa5 100644 --- a/jquery.placeholder.min.js +++ b/jquery.placeholder.min.js @@ -3,4 +3,4 @@ * @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(d){if(d.val()===''||d.val()===d.attr('placeholder')){d.addClass('placeholder').val(d.attr('placeholder'))}else{d.removeClass('placeholder')}}function c(d){if(d.has('.placeholder')){d.find('.placeholder:first').val('').focus().removeClass('placeholder');return !1}}return a(this).each(function(){var d=a(this);if(d.is(':password')||!d.is(':input')){return}b(d);a(this.form).submit(function(){c(a(this))}).find('input[type=submit]').click(function(){c(this.form)});d.focusin(function(){if(d.val()===d.attr('placeholder')){d.removeClass('placeholder').val('')}}).focusout(function(){b(d)})})}})(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([placeholder])').click(function(){if(a(this).find('.placeholder').length){a(this).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