Fix for "Show password" for Drupal

  1. /**
  2.  * Add a "Show password" checkbox to each password field.
  3.  */
  4. Drupal.behaviors.showPassword = function (context) {
  5.   // Create the checkbox.
  6.   var showPassword = $('<label><input type="checkbox" />' + Drupal.t('Show password') + '</label>');
  7.   // Add click handler.
  8.   $(':checkbox', showPassword).click(function () {
  9.     var orig;
  10.     var copy;
  11.     if ($(this).is(':checked')) {
  12.       // Copy original field and convert it to a simple textfield.
  13.       orig = $(this).parent().parent().find(':password');
  14.       copy = orig.clone();
  15.       $(copy).attr('type', 'text');
  16.       $(copy).addClass('show-password');
  17.     }
  18.     else {
  19.       // Copy original field and convert it to a password field.
  20.       orig = $(this).parent().parent().find('input.show-password');
  21.       $(orig).removeClass('show-password');
  22.       copy = orig.clone();
  23.       $(copy).attr('type', 'password');
  24.     }
  25.     // Replace currently displayed field with the modified copy.
  26.     $(orig).replaceWith(copy);
  27.   });
  28.   // Add checkbox to all password field on the current page.
  29.   showPassword.insertAfter($(':password'));
  30. };

Submit Fix

Any tags you'd like to associate with your code, delimitered by commas (example: Views, CCK, Module, etc).
Select the syntax highlighting mode to use.