From 593350d41a2b77040862b96804a6f8470de0e325 Mon Sep 17 00:00:00 2001 From: temp01 Date: Wed, 6 Oct 2010 14:27:59 -0700 Subject: [PATCH] Optimization, better arguments copying for password inputs, jQuery 1.3 compatibility and more! --- jquery.placeholder.js | 61 +++++++++++++++++++++++-------------------- 1 file changed, 33 insertions(+), 28 deletions(-) diff --git a/jquery.placeholder.js b/jquery.placeholder.js index 888d4b7..91862a4 100644 --- a/jquery.placeholder.js +++ b/jquery.placeholder.js @@ -12,24 +12,22 @@ return; } - 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 args(elem) { + // Return an object of element attributes + var newAttrs = {}, rinlinejQuery = /^jQuery\d+$/; + $.each(elem.attributes, function(i, attr){ + if( !rinlinejQuery.test(attr.name) ){ + newAttrs[attr.name] = attr.value; + } + }); + return newAttrs; } 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(); + $input.hide().next().show().focus(); } else { $input.val('').removeClass('placeholder'); } @@ -38,21 +36,28 @@ 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' })); + $input = $(this); + if ($input.val() === '' || $input.val() === $input.attr('placeholder')) { + if ($input.is(':password')) { + if(!$input.data('placeholder-textinput')){ + try { + $replacement = $input.clone().attr({ type: 'text' }); + } catch(e) { + $replacement = $('').attr( $.extend(args($input[0]), {type: 'text'}) ); + } + $replacement + .removeAttr('name') + .data('placeholder-password', true) + .bind('focus.placeholder', onFocus); + $input + .data('placeholder-textinput', $replacement) + .before($replacement); } - $replacement.data('placeholder-password', true).bind('focus.placeholder', onFocus); - $elem.hide().before($replacement); - $elem = $replacement; + $input = $input.hide().prev().show(); } - $elem.addClass('placeholder').val($elem.attr('placeholder')); + $input.addClass('placeholder').val($input.attr('placeholder')); } else { - $elem.removeClass('placeholder'); + $input.removeClass('placeholder'); } } @@ -70,10 +75,10 @@ }); $.fn.placeholder = function() { - return this.filter(':input[placeholder]').bind({ - 'focus.placeholder': onFocus, - 'blur.placeholder': setPlaceholder - }).trigger('blur.placeholder').end(); + return this.filter(':input[placeholder]') + .bind('focus.placeholder', onFocus) + .bind('blur.placeholder' , setPlaceholder) + .trigger('blur.placeholder').end(); }; })(jQuery); \ No newline at end of file