Created
January 11, 2020 17:17
-
-
Save bsolomon1124/4dafac2948e8cf81ef9d6424ea5f6c4d to your computer and use it in GitHub Desktop.
Toggle <input type="password"> visibility via jQuery
This file contains hidden or 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
| <!DOCTYPE html> | |
| <html lang="en-US"> | |
| <head> | |
| <meta charset="utf-8"> | |
| <title>Toggle Password Visibility Demo</title> | |
| <script | |
| src="https://code.jquery.com/jquery-3.4.1.min.js" | |
| integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo=" | |
| crossorigin="anonymous"></script> | |
| </head> | |
| <body> | |
| Password 1: | |
| <input type="password"> | |
| <br> | |
| Password 2: | |
| <input type="password"> | |
| <script> | |
| // Add a checkbox sibling to each password input. When | |
| // the checkbox is clicked, toggle the visibility of the | |
| // password input. | |
| $(function() { | |
| $("input[type='password']").each(function(index) { | |
| $(this).after('<input type="checkbox"><span>show</span>'); | |
| $(this).next().click(function() { | |
| let $pbox = $(this).prev(); | |
| if ($pbox.attr("type") === "password") { | |
| $pbox.attr("type", "text"); | |
| } else { | |
| $pbox.attr("type", "password"); | |
| } | |
| }); | |
| }); | |
| }); | |
| </script> | |
| </body> | |
| </html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment