From 3eec0df99d2fe79b9234f8c7254fd8e91db68a2a Mon Sep 17 00:00:00 2001 From: Jordan Brown Date: Thu, 13 Jun 2013 18:59:29 +0000 Subject: [PATCH] Don't remove class if the input is equal to the placeholder text. This should fix issue #109 upstream. --- jquery.placeholder.js | 45 +++++++++++++++++++++++++++++-------------- 1 file changed, 31 insertions(+), 14 deletions(-) diff --git a/jquery.placeholder.js b/jquery.placeholder.js index 0512ca5..3b5cb6f 100644 --- a/jquery.placeholder.js +++ b/jquery.placeholder.js @@ -115,21 +115,33 @@ } } } - function setPlaceholder() { - var $replacement, - input = this, - $input = $(input), - $origInput = $input, - id = this.id; - if (input.value == '') { - if (input.type == 'password') { + var + $replacement, + input, + $input, + id; + + id = this.id; + input = this; + $input = $(input); + + if (input.value === '') { + if (input.type === 'password') { if (!$input.data('placeholder-textinput')) { try { - $replacement = $input.clone().attr({ 'type': 'text' }); + $replacement = $input + .clone() + .attr({ + type: 'text' + }); } catch(e) { - $replacement = $('').attr($.extend(args(this), { 'type': 'text' })); + $replacement = $('') + .attr($.extend(args(this), { + type: 'text' + })); } + $replacement .removeAttr('name') .data({ @@ -137,6 +149,7 @@ 'placeholder-id': id }) .bind('focus.placeholder', clearPlaceholder); + $input .data({ 'placeholder-textinput': $replacement, @@ -144,14 +157,18 @@ }) .before($replacement); } - $input = $input.removeAttr('id').hide().prev().attr('id', id).show(); + $input = $input + .removeAttr('id') + .hide() + .prev() + .attr('id', id) + .show(); // Note: `$input[0] != input` now! } $input.addClass('placeholder'); $input[0].value = $input.attr('placeholder'); - } else { + } else if (input.value !== $input.attr('placeholder')) { $input.removeClass('placeholder'); } } - -}(this, document, jQuery)); \ No newline at end of file +}(this, document, jQuery));