Created
January 9, 2019 20:26
-
-
Save coorasse/7569683ca1aa841500d5e73cf40ba9f8 to your computer and use it in GitHub Desktop.
Input switch from password to text
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<input id="api-token" type="password" value="" readonly data-select /> | |
<button data-token-reveal="api-token" data-alternate-text="Hide">Show</button> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$('[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(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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