Skip to content

Commit

Permalink
Optimization, better arguments copying for password inputs, jQuery 1.…
Browse files Browse the repository at this point in the history
…3 compatibility and more!
  • Loading branch information
temp01 committed Oct 6, 2010
1 parent bd6b797 commit 593350d
Showing 1 changed file with 33 additions and 28 deletions.
61 changes: 33 additions & 28 deletions jquery.placeholder.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,22 @@
return;
}

function args($elem) {
// Get attributes string from outerHTML
var html = $('<div>').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');
}
Expand All @@ -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 = $('<input>', $.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 = $('<input>').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');
}
}

Expand All @@ -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);

0 comments on commit 593350d

Please sign in to comment.