Permalink
Browse files

Initial work on password support

  • Loading branch information...
danbentley committed Jun 11, 2011
1 parent 3cea4d3 commit 56ec0eb1fe02a5dcde82b6baeef090e163bc364a
Showing with 57 additions and 5 deletions.
  1. +57 −5 js/jquery.placeholder.js
View
@@ -13,34 +13,86 @@
if ("placeholder" in document.createElement("input")) return;
$(document).ready(function(){
- $(':input[placeholder]').each(function() {
+ $(':input[placeholder]').not(':password').each(function() {
setupPlaceholder($(this));
});
+
+ $(':password').each(function() {
+ setupPasswords($(this));
+ });
$('form').submit(function(e) {
clearPlaceholdersBeforeSubmit($(this));
});
});
+ function setupPasswords(input) {
+
+ var passwordPlaceholder = createPasswordPlaceholder(input);
+
+ input.after(passwordPlaceholder);
+
+ (input.val() === '') ? input.hide() : passwordPlaceholder.hide();
+
+ $(input).blur(function(e) {
+ if (input.val() === '') {
+ input.toggle().focus();
+ passwordPlaceholder.toggle();
+ }
+ });
+
+ $(passwordPlaceholder).focus(function(e) {
+ input.toggle().focus();
+ passwordPlaceholder.toggle();
+ });
+ }
+
+ function createPasswordPlaceholder(input) {
+
+ return input.clone().attr({
+ name: 'password-placeholder' + input.attr('name'),
+ id: 'password-placeholder' + input.attr('name'),
+ value: input.attr('placeholder'),
+ type: 'input',
+ readonly: true
+ });
+ }
+
function setupPlaceholder(input) {
var placeholderText = input.attr('placeholder');
- if (input.val() === '') input.val(placeholderText);
+ setPlaceholderOrFlagChanged(input, placeholderText);
input.bind({
focus: function(e) {
- if (input.val() === placeholderText) input.val('');
+ if (input.val() === placeholderText && input.data('changed') === undefined) input.val('');
},
blur: function(e) {
if (input.val() === '') input.val(placeholderText);
+ },
+ change: function(e) {
+ if (input.val() !== '') input.data('changed', true);
}
});
}
+ function setPlaceholderOrFlagChanged(input, placeholderText) {
+ if (input.val() === '') {
+ input.val(placeholderText);
+ } else {
+ input.data('changed', true);
+ }
+ }
+
function clearPlaceholdersBeforeSubmit(form) {
- form.find(':input[placeholder]').each(function() {
+
+ form.find(':input[name^=password-placeholder]').remove();
+
+ var inputs = form.find(':input[placeholder]');
+ inputs.each(function() {
var el = $(this);
- if (el.val() === el.attr('placeholder')) el.val('');
+ if (el.val() === el.attr('placeholder') && el.data('changed') === undefined) el.val('');
});
+
}
})(jQuery);

0 comments on commit 56ec0eb

Please sign in to comment.