Skip to content

Instantly share code, notes, and snippets.

@coorasse
Created January 9, 2019 20:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save coorasse/7569683ca1aa841500d5e73cf40ba9f8 to your computer and use it in GitHub Desktop.
Save coorasse/7569683ca1aa841500d5e73cf40ba9f8 to your computer and use it in GitHub Desktop.
Input switch from password to text
<input id="api-token" type="password" value="" readonly data-select />
<button data-token-reveal="api-token" data-alternate-text="Hide">Show</button>
$('[data-token-reveal]').on('click', function (el) {
const alternateText = $(this).data('alternate-text');
const input = document.getElementById($(this).data('token-reveal'));
if (input.type === 'password') {
input.type = 'text';
} else {
input.type = 'password';
}
$(this).data('alternate-text', $(this).text());
$(this).text(alternateText);
});
$('[data-select]').on('click', function () {
$(this).select();
});
@coorasse
Copy link
Author

coorasse commented Jan 9, 2019

An example on how to create an input field with a token or a password and a button to show/hide its content. In addition, when the user clicks on the input field, the text is automatically selected to simplify the process of copy/paste

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment