Skip to content

Instantly share code, notes, and snippets.

@stgogm
Created April 27, 2013 20:16
Show Gist options
  • Select an option

  • Save stgogm/5474528 to your computer and use it in GitHub Desktop.

Select an option

Save stgogm/5474528 to your computer and use it in GitHub Desktop.
This is a simple code for toggling password input fields into text fields and vice versa. You must provide a listener element, like a check box, and the inputs ID you want to toggle as 'passwordInputA, passwordInputB, ...'.
function PasswordsVisible(listener, fields) {
this.obscure = true;
this.fields = fields;
this.listener = $('#' + listener);
this.length = 0;
this.init();
}
PasswordsVisible.prototype = {
init : function() {
var me = this;
me.fields = me.fields.split(', ');
me.length = me.fields.length;
me.listener.on('click', function() {
me.toggle();
});
},
toggle : function() {
var me = this;
var type = '';
if (me.obscure) {
me.obscure = false;
type = 'text';
} else {
me.obscure = true;
type = 'password';
}
for(var i = 0; i < me.length; i++) {
$('input#' + me.fields[i]).attr('type', type);
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment