Because life's too short to waste time re-typing passwords.
Because life's too short to waste time re-typing passwords.
[!IMPORTANT] This plugin is no longer maintained. See our article Hide/Show Passwords: Ten Years Later for more info.
Because life's too short to waste time re-typing passwords.
Inspired by a pattern seen in Polar, IE 10+ and LinkedIn and documented by Luke W, hideShowPassword lets you easily hide and show passwords via JavaScript or a nifty inset toggle.
The plugin works in any browser that supports resetting the type attribute of `` elements (pretty much everything newer than IE8). The plugin should fall back gracefully in cases where this is not supported.
Include the plugin after you've included jQuery:
If Modernizr is also included, the plugin's touch enhancements will default to the value of Modernizr.touchevents.
npm install --save jquery
npm install --save hideshowpasswordvar $ = require('jquery');
require('hideshowpassword');bower install hideshowpasswordThe plugin acts on `` elements (typically password fields):
You can quickly show, hide or toggle the visibility of the field's contents:
$('#password').showPassword(); // Reveal
$('#password').hidePassword(); // Hide
$('#password').togglePassword(); // ToggleThese are all shorthand versions of the hideShowPassword method:
$('#password').hideShowPassword(true); // Reveal
$('#password').hideShowPassword(false); // Hide
$('#password').hideShowPassword('toggle'); // ToggleThe inner toggle functionality tends to steal the show for this plugin. You can pass it along as the second argument of the hideShowPassword method:
// Reveal password by default and create an inner toggle
$('#password').hideShowPassword(true, true);Or as the first argument of any of the shorthand methods:
// Reveal password by default and create an inner toggle
$('#password').showPassword(true);If you would like the inner toggle to be hidden until a specific event, you can pass along that event as a string instead:
// Hide the inner toggle till the #password element is focused
$('#password').showPassword('focus');Any additional options may be modified by passing along an object as the last argument of any of the aformentioned methods. Here's the previous example, but with a custom class for the toggle:
$('#password').hideShowPassword(true, 'focus', {
toggle: {
className: 'my-toggle'
}
});In fact, we could pass along all of these arguments as a single object if we want:
$('#password').hideShowPassword({
show: true,
innerToggle: 'focus',
toggle: {
className: 'my-toggle'
}
});There are many options available if your project's needs are particularly unique.
If you need to respond to changes to the password field's visibility, you can use any of the following events:
$('#password')
.on('hideShowPasswordInit', function(){
console.log('plugin initialized');
})
.on('passwordVisibilityChange', function(){
console.log('password shown or hidden');
})
.on('passwordShown', function(){
console.log('password shown');
})
.on('passwordHidden', function(){
console.log('password hidden');
});Here are all of the available options and their defaults:
…You may override these defaults by manipulating the $.fn.hideShowPassword.defaults object:
$.extend(true, $.fn.hideShowPassword.defaults, {
states: {
shown: {
toggle: {
content: "esconder"
}
},
hidden: {
toggle: {
content: "espectáculo"
}
}
}
});Internet Explorer 10 introduced its own controls for password and text input fields that sometimes compete with the inner toggle functionality of this plugin. Thankfully, they are easily overwritten using CSS:
::-ms-reveal,
::-ms-clear {
display: none !important;
}For some reason the plugin returns a false positive when feature-testing unless honest-to-goodness IE8 or earlier is used.
If you use the inner toggle feature on an invisible element, it may not have enough information to correctly style the wrapper and toggle elements. It's recommended that you delay instantiation of the plugin until the elements are visible.
Here's a hypothetical example using a Bootstrap modal:
$('#my-modal').on('shown.bs.modal', function (event) {
$('#password').showPassword(true);
});triggerOnToggle feature (#56)title attributes to toggle by default (#41)inheritStyles bug (#34)console.log call (#13)className option now instantiates on init (#11)Released under the MIT License.
This repository contains other libraries that may or may not fall under the same license:
No open issues yet, or sync has not completed.