Show Password 1.0

Created Wednesday 11th of March 2009 by Andreas Lagerkvist
Copyright © 2008 Andreas Lagerkvist (andreaslagerkvist.com)

What it Does

This little plug inserts a "View password"-checkbox next to inputs of type password that allows the user to toggle the password's visibility. When the checkbox is checked the password is displayed in plain text.

How to Use

jQuery('#my-form').showPassword(); would insert "password togglers" in every input[type=password] within #my-form.

Example

Example Code

HTML

<div id="jquery-show-password-example">
<
input type="password" value="oioioi"/>
</
div>

JS

jQuery('#jquery-show-password-example').showPassword();

Source Code

jQuery.fn.showPassword = function (conf) {
    var 
config = $.extend({
        
str:        'Show password'
        
className:    'password-toggler'
    
}, conf);

    return 
this.each(function () {
        
jQuery('input[type=password]'this).each(function () {
            var 
field        jQuery(this);
            var 
fakeField    jQuery('<input type="text" class="' config.className '" value="' field.val() + '" />').insertAfter(field).hide(); // only IE really needs this
            
var check        jQuery('<label class="' config.className '"><input type="checkbox" /> ' config.str '</label>');
            var 
parentLabel    field.parents('label');

            if (
parentLabel.length) {
                
check.insertAfter(parentLabel);
            }
            else {
                
check.insertAfter(fakeField); // field
            
}

            
check.find('input').click(function() {
                if (
jQuery(this).is(':checked')) {
                
//    field.attr('type', 'text'); // strange, this threw errors
                //    field[0].type = 'text'; // and this doesn't work in IE
                    
field.hide();
                    
fakeField.val(field.val()).show();
                }
                else {
                
//    field.attr('type', 'password');
                //    field[0].type = 'password';
                    
field.show();
                    
fakeField.hide();
                }
            });

            
fakeField.change(function() {
                
field.val(fakeField.val());
            });
        });
    });
};

Download

Plug-in

Requires

blog comments powered by Disqus

Random jQuery Plug-ins

  • Live Search

    Use this plug-in to turn a normal form-input in to a live ajax search widget. The plug-in displays any HTML you like in the results and the search-res...

    Details

  • Content Scroller

    Use this plug-in to make a list of elements only show one at a time and every now and then scroll to the next one. The plug-in scrolls in infinty, sta...

    Details

  • Form Hints

    Using this plug-in you can add a descriptive hint to any form-control you may have on your site.
    Add a hint by giving the form-control a title-attri...

    Details

More Plug-ins

Recent Comments

Powered by Disqus
Page cached. Loaded in: 1.5635 second(s).
Last DB change: 2012-04-02 11:06:05
Last file change: 2012-04-25 20:30:39
Cache created: 2012-05-18 02:33:04