• Stars
    star
    736
  • Rank 61,564 (Top 2 %)
  • Language
    JavaScript
  • Created over 11 years ago
  • Updated over 4 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Because life's too short to waste time re-typing passwords.

hideShowPassword

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 <input> elements (pretty much everything newer than IE8). The plugin should fall back gracefully in cases where this is not supported.

Installation

Include the plugin after you've included jQuery:

<script src="//code.jquery.com/jquery-2.1.3.min.js"></script>
<script src="path/to/hideShowPassword.min.js"></script>

If Modernizr is also included, the plugin's touch enhancements will default to the value of Modernizr.touchevents.

Using npm and Browserify

npm install --save jquery
npm install --save hideshowpassword
var $ = require('jquery');
require('hideshowpassword');

Using Bower

bower install hideshowpassword
<script src="//code.jquery.com/jquery-2.1.3.min.js"></script>
<script src="bower_components/hideShowPassword/hideShowPassword.min.js"></script>

Usage

The plugin acts on <input> elements (typically password fields):

<input id="password" type="password">

Showing, hiding and toggling

You can quickly show, hide or toggle the visibility of the field's contents:

$('#password').showPassword();    // Reveal
$('#password').hidePassword();    // Hide
$('#password').togglePassword();  // Toggle

These are all shorthand versions of the hideShowPassword method:

$('#password').hideShowPassword(true);      // Reveal
$('#password').hideShowPassword(false);     // Hide
$('#password').hideShowPassword('toggle');  // Toggle

Enabling the inner toggle button

The 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');

Specifying more options

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.

Events

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');
  });

Options

Here are all of the available options and their defaults:

.hideShowPassword({
  // Visibility of the password text. Can be true, false, 'toggle'
  // or 'infer'. If 'toggle', it will be the opposite of whatever
  // it currently is. If 'infer', it will be based on the input
  // type (false if 'password', otherwise true).
  show: 'infer',

  // Set to true to create an inner toggle for this input. Can
  // also be sent to an event name to delay visibility of toggle
  // until that event is triggered on the input element.
  innerToggle: false,

  // If false, the plugin will be disabled entirely. Set to
  // the outcome of a test to insure input attributes can be
  // set after input has been inserted into the DOM.
  enable: canSetInputAttribute,

  // Event to trigger whenever the element is toggled.
  // For example, if 'focus' it will focus the cursor in the
  // input element after toggling.
  triggerOnToggle: false,

  // Class to add to input element when the plugin is enabled.
  className: 'hideShowPassword-field',

  // Event to trigger when the plugin is initialized and enabled.
  initEvent: 'hideShowPasswordInit',

  // Event to trigger whenever the visibility changes.
  changeEvent: 'passwordVisibilityChange',

  // Properties to add to the input element.
  props: {
    autocapitalize: 'off',
    autocomplete: 'off',
    autocorrect: 'off',
    spellcheck: 'false'
  },

  // Options specific to the inner toggle.
  toggle: {
    // The element to create.
    element: '<button type="button">',
    // Class name of element.
    className: 'hideShowPassword-toggle',
    // Whether or not to support touch-specific enhancements.
    // Defaults to the value of Modernizr.touchevents if available,
    // otherwise false.
    touchSupport: (typeof Modernizr === 'undefined') ? false : Modernizr.touchevents,
    // Non-touch event to bind to.
    attachToEvent: 'click.hideShowPassword',
    // Event to bind to when touchSupport is true.
    attachToTouchEvent: 'touchstart.hideShowPassword mousedown.hideShowPassword',
    // Key event to bind to if attachToKeyCodes is an array
    // of at least one keycode.
    attachToKeyEvent: 'keyup',
    // Key codes to bind the toggle event to for accessibility.
    // If false, this feature is disabled entirely.
    // If true, the array of key codes will be determined based
    // on the value of the element option.
    attachToKeyCodes: true,
    // Styles to add to the toggle element. Does not include
    // positioning styles.
    styles: { position: 'absolute' },
    // Styles to add only when touchSupport is true.
    touchStyles: { pointerEvents: 'none' },
    // Where to position the inner toggle relative to the
    // input element. Can be 'right', 'left' or 'infer'. If
    // 'infer', it will be based on the text-direction of the
    // input element.
    position: 'infer',
    // Where to position the inner toggle on the y-axis
    // relative to the input element. Can be 'top', 'bottom'
    // or 'middle'.
    verticalAlign: 'middle',
    // Amount by which to "offset" the toggle from the edge
    // of the input element.
    offset: 0,
    // Attributes to add to the toggle element.
    attr: {
      role: 'button',
      'aria-label': 'Show Password',
      title: 'Show Password',
      tabIndex: 0
    }
  },

  // Options specific to the wrapper element, created
  // when the innerToggle is initialized to help with
  // positioning of that element.
  wrapper: {
    // The element to create.
    element: '<div>',
    // Class name of element.
    className: 'hideShowPassword-wrapper',
    // If true, the width of the wrapper will be set
    // unless it is already the same width as the inner
    // element. If false, the width will never be set. Any
    // other value will be used as the width.
    enforceWidth: true,
    // Styles to add to the wrapper element. Does not
    // include inherited styles or width if enforceWidth
    // is not false.
    styles: { position: 'relative' },
    // Styles to "inherit" from the input element, allowing
    // the wrapper to avoid disrupting page styles.
    inheritStyles: [
      'display',
      'verticalAlign',
      'marginTop',
      'marginRight',
      'marginBottom',
      'marginLeft'
    ],
    // Styles for the input element when wrapped.
    innerElementStyles: {
      marginTop: 0,
      marginRight: 0,
      marginBottom: 0,
      marginLeft: 0
    }
  },

  // Options specific to the 'shown' or 'hidden'
  // states of the input element.
  states: {
    shown: {
      className: 'hideShowPassword-shown',
      changeEvent: 'passwordShown',
      props: { type: 'text' },
      toggle: {
        className: 'hideShowPassword-toggle-hide',
        content: 'Hide',
        attr: {
          'aria-pressed': 'true'
          title: 'Hide Password',
        }
      }
    },
    hidden: {
      className: 'hideShowPassword-hidden',
      changeEvent: 'passwordHidden',
      props: { type: 'password' },
      toggle: {
        className: 'hideShowPassword-toggle-show',
        content: 'Show',
        attr: {
          'aria-pressed': 'false',
          title: 'Show Password',
        }
      }
    }
  }

});

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"
      }
    }
  }
});

Known Issues

Competing controls in IE10+ (Windows 8)

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;
}

Error when debugging IE8 or earlier in IE9+ Developer Tools

For some reason the plugin returns a false positive when feature-testing unless honest-to-goodness IE8 or earlier is used.

Toggle quirks in invisible elements

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);
});

History

  • 2.2.0: Expose defaults for easier overrides (#60, #61)
  • 2.1.0: Add triggerOnToggle feature (#56)
  • 2.0.11: Fix for Bower install on Windows (#44)
  • 2.0.10: Update Modernizr test (#42)
  • 2.0.9: Add title attributes to toggle by default (#41)
  • 2.0.8: Fixing bloated bundles bug (#39)
  • 2.0.7: Fixing inheritStyles bug (#34)
  • 2.0.6: Revising npm package name (#28)
  • 2.0.5: Revising npm package repo URL (#28)
  • 2.0.4: Namespaced events (#20), npm support (#21)
  • 2.0.3: Removed errant console.log call (#13)
  • 2.0.2: className option now instantiates on init (#11)
  • 2.0.1: Fix for missing innerElementStyles
  • 2.0.0: Major rewrite with better accessibility and deeper options
  • 1.0.3: Added wrapperWidth option
  • 1.0.2: Uses deep merge for options
  • 1.0.1: Added AMD support
  • 1.0.0: Voila!

License

Released under the MIT License.

This repository contains other libraries that may or may not fall under the same license:

More Repositories

1

drizzle

A streamlined tool for developing, documenting and presenting UI pattern libraries.
HTML
666
star
2

lighthouse-parade

A Node.js command line tool that crawls a domain and gathers lighthouse performance data for every page.
TypeScript
358
star
3

SimpleSlideView

A nifty little jQuery or Zepto plugin for the simplest of sliding views.
HTML
258
star
4

pleasantest

⛵ Write real-browser tests with Testing Library in Jest
TypeScript
172
star
5

simple-svg-placeholder

A very simple placeholder image generator with zero dependencies.
JavaScript
170
star
6

offCanvasMenu

A jQuery/Zepto plugin that provides an easy way to implement an off-canvas toggling menu, a navigation metaphor made popular by mobile applications.
CoffeeScript
139
star
7

pwastats

A directory of Progressive Web App case studies.
HTML
101
star
8

image-compare

A web component for comparing images with a slider.
HTML
62
star
9

transition-hidden-element

A JavaScript utility to help you use CSS transitions when showing and hiding elements with the `hidden` attribute.
JavaScript
52
star
10

elastic-textarea

A web component for progressively-enhanced auto-expanding textareas
JavaScript
35
star
11

image-breakpoints

Given a source image, generate a series of related images at various file sizes.
PHP
28
star
12

guides

Conventions, processes and notes about how we do things
24
star
13

cloudfour.com-patterns

The design system, component library and documentation for cloudfour.com and related projects
MDX
24
star
14

core-hbs-helpers

Handlebars helpers that we usually need
JavaScript
11
star
15

leveller

A jQuery plugin for equalizing element heights.
HTML
10
star
16

lite-model-viewer

A lazy-loaded model-viewer web component
HTML
9
star
17

core-gulp-tasks

⛔️ DEPRECATED Gulp tasks that we usually need
JavaScript
9
star
18

svg-icon-example

JavaScript
8
star
19

constraint-validation-api-demo

Astro
7
star
20

pwa-workshop

The Progressive Web App workshop files
HTML
7
star
21

svg-icon-stress-test

Test page accompanying an article about performance of way too many SVG icons at once.
JavaScript
7
star
22

drizzle-builder

The tool for building a Drizzle.
JavaScript
6
star
23

slides-svg-101

SVG! ♥‿♥
JavaScript
5
star
24

tpp-research-monorepo

JavaScript
5
star
25

stylelint-config-cloudfour-suit

Cloud Four's shareable config + SUIT naming
JavaScript
4
star
26

eleventy-patterns

(WIP) A boilerplate template used to quickly generate pattern libraries and prototyping environments
JavaScript
4
star
27

eslint-config

JavaScript
4
star
28

smashing-mag-sw

CSS
2
star
29

stylelint-config-cloudfour

Cloud Four's shareable config
JavaScript
2
star
30

code-transform-sandbox

TypeScript
1
star
31

pleasantest-site-sandbox

A design and prototyping sandbox for the new Pleasantest site
Nunjucks
1
star
32

generator-drizzle

Yeoman generator for Drizzle-based projects
JavaScript
1
star
33

generator-core-webapp

Yeoman Generator for core web apps
JavaScript
1
star
34

workshop-here-everywhere

Workshop: How to get from Here to Everywhere (Taking your web skills mobile)
PHP
1
star
35

vite-vue-testing-library

Adding Vue Testing Library to a Vite + Vue 3 installation
Vue
1
star
36

scaffolding-example

A demo of how to write your own automated scaffolding script
JavaScript
1
star
37

sky-nav-cls-pe

Demos for the Sky Nav Cumulative Layout Shift + Progressive Enhancement article
Astro
1
star
38

js-sunrise

Study material and workshop for JavaScript
JavaScript
1
star
39

stencil-backbone-example

TypeScript
1
star