Показать и скрыть пароль с помощью JavaScript
Опубликовано: 14 Декабря, 2021
При заполнении формы возникает ситуация, когда мы вводим пароль и хотим увидеть, что мы ввели до сих пор. Чтобы увидеть это, есть флажок, при нажатии на который символы становятся видимыми.
В этом посте эта функция, т.е. переключение пароля, реализована с помощью JavaScript.
Algorithm
1)Create a HTML form which contain an input field of type password.2)Create a checkbox which will be responsible for toggling.
3)Create a function which will response for toggling when a user clicks on the checkbox.
Примеры:
Password is geeksforgeeks.
So, on typing it will show like this *************
And on clicking the checkbox it will show the characters: geeksforgeeks.
<!DOCTYPE html> <html> <body> <b><p>Click on the checkbox to show or hide password: </p></b> <b>Password</b>: <input type= "password" value= "geeksforgeeks" id= "typepass" > <input type= "checkbox" onclick= "Toggle()" > <b>Show Password</b> <script> // Change the type of input to password or text function Toggle() { var temp = document.getElementById( "typepass" ); if (temp.type === "password" ) { temp.type = "text" ; } else { temp.type = "password" ; } } </script> </body> </html> |
Выход:
- Скрыть пароль:
- Показать пароль: