From 06d8e67996afa61bc5dc5e474a5e508b9cf29188 Mon Sep 17 00:00:00 2001 From: Mathias Bynens Date: Sat, 24 Dec 2011 10:36:46 +0100 Subject: [PATCH] Make submit handler work for dynamically inserted forms MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes #41. This plugin now requires jQuery 1.4.2 (since we’re using `jQuery#delegate`). --- README.md | 1 + jquery.placeholder.js | 4 ++-- jquery.placeholder.min.js | 4 ++-- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index e18303e..43fbb3b 100644 --- a/README.md +++ b/README.md @@ -35,6 +35,7 @@ input, textarea { color: #000; } ## Notes +* Requires jQuery 1.4.2+. * Works in all A-grade browsers, including IE6. * Automatically checks if the browser natively supports the HTML5 `placeholder` attribute for `input` and `textarea` elements. If this is the case, the plugin won’t do anything. If `@placeholder` is only supported for `input` elements, the plugin will leave those alone and apply to `textarea`s exclusively. (This is the case for Safari 4, Opera 11.00, and possibly other browsers.) * Caches the results of its two feature tests in `jQuery.fn.placeholder.input` and `jQuery.fn.placeholder.textarea`. For example, if `@placeholder` is natively supported for `input` elements, `jQuery.fn.placeholder.input` will be `true`. After loading the plugin, you can re-use these properties in your own code. diff --git a/jquery.placeholder.js b/jquery.placeholder.js index dd34bb0..b4bbc7c 100644 --- a/jquery.placeholder.js +++ b/jquery.placeholder.js @@ -1,4 +1,4 @@ -/*! http://mths.be/placeholder v1.8.6 by @mathias */ +/*! http://mths.be/placeholder v1.8.7 by @mathias */ ;(function(window, document, $) { var isInputSupported = 'placeholder' in document.createElement('input'), @@ -30,7 +30,7 @@ $(function() { // Look for forms - $('form').bind('submit.placeholder', function() { + $(document).delegate('form', 'submit.placeholder', function() { // Clear the placeholder values so they don’t get submitted var $inputs = $('.placeholder', this).each(clearPlaceholder); setTimeout(function() { diff --git a/jquery.placeholder.min.js b/jquery.placeholder.min.js index d5fa02f..89b01b7 100644 --- a/jquery.placeholder.min.js +++ b/jquery.placeholder.min.js @@ -1,2 +1,2 @@ -/*! http://mths.be/placeholder v1.8.6 by @mathias */ -(function(e,g,$){var a='placeholder' in g.createElement('input'),c='placeholder' in g.createElement('textarea'),h=$.fn,i;if(a&&c){i=h.placeholder=function(){return this};i.input=i.textarea=true}else{i=h.placeholder=function(){return this.filter((a?'textarea':':input')+'[placeholder]').not('.placeholder').bind('focus.placeholder',b).bind('blur.placeholder',d).trigger('blur.placeholder').end()};i.input=a;i.textarea=c;$(function(){$('form').bind('submit.placeholder',function(){var j=$('.placeholder',this).each(b);setTimeout(function(){j.each(d)},10)})});$(e).bind('unload.placeholder',function(){$('.placeholder').val('')})}function f(k){var j={},l=/^jQuery\d+$/;$.each(k.attributes,function(n,m){if(m.specified&&!l.test(m.name)){j[m.name]=m.value}});return j}function b(){var j=$(this);if(j.val()===j.attr('placeholder')&&j.hasClass('placeholder')){if(j.data('placeholder-password')){j.hide().next().show().focus().attr('id',j.removeAttr('id').data('placeholder-id'))}else{j.val('').removeClass('placeholder')}}}function d(){var n,m=$(this),j=m,l=this.id;if(m.val()===''){if(m.is(':password')){if(!m.data('placeholder-textinput')){try{n=m.clone().attr({type:'text'})}catch(k){n=$('').attr($.extend(f(this),{type:'text'}))}n.removeAttr('name').data('placeholder-password',true).data('placeholder-id',l).bind('focus.placeholder',b);m.data('placeholder-textinput',n).data('placeholder-id',l).before(n)}m=m.removeAttr('id').hide().prev().attr('id',l).show()}m.addClass('placeholder').val(m.attr('placeholder'))}else{m.removeClass('placeholder')}}}(this,document,jQuery)); \ No newline at end of file +/*! http://mths.be/placeholder v1.8.7 by @mathias */ +(function(f,h,c){var a='placeholder' in h.createElement('input'),d='placeholder' in h.createElement('textarea'),i=c.fn,j;if(a&&d){j=i.placeholder=function(){return this};j.input=j.textarea=true}else{j=i.placeholder=function(){return this.filter((a?'textarea':':input')+'[placeholder]').not('.placeholder').bind('focus.placeholder',b).bind('blur.placeholder',e).trigger('blur.placeholder').end()};j.input=a;j.textarea=d;c(function(){c(h).delegate('form','submit.placeholder',function(){var k=c('.placeholder',this).each(b);setTimeout(function(){k.each(e)},10)})});c(f).bind('unload.placeholder',function(){c('.placeholder').val('')})}function g(l){var k={},m=/^jQuery\d+$/;c.each(l.attributes,function(o,n){if(n.specified&&!m.test(n.name)){k[n.name]=n.value}});return k}function b(){var k=c(this);if(k.val()===k.attr('placeholder')&&k.hasClass('placeholder')){if(k.data('placeholder-password')){k.hide().next().show().focus().attr('id',k.removeAttr('id').data('placeholder-id'))}else{k.val('').removeClass('placeholder')}}}function e(){var o,n=c(this),k=n,m=this.id;if(n.val()===''){if(n.is(':password')){if(!n.data('placeholder-textinput')){try{o=n.clone().attr({type:'text'})}catch(l){o=c('').attr(c.extend(g(this),{type:'text'}))}o.removeAttr('name').data('placeholder-password',true).data('placeholder-id',m).bind('focus.placeholder',b);n.data('placeholder-textinput',o).data('placeholder-id',m).before(o)}n=n.removeAttr('id').hide().prev().attr('id',m).show()}n.addClass('placeholder').val(n.attr('placeholder'))}else{n.removeClass('placeholder')}}}(this,document,jQuery)); \ No newline at end of file