Saturday, June 13, 2026
HomeLanguagesJavascriptShow and hide password using JavaScript

Show and hide password using JavaScript

This article will show you how to Hide/Show passwords using JavaScript. While filling up a form, there comes a situation where we type a password and want to see what we have typed till now. To see that, there is a checkbox click on which makes the characters visible. In this article, we will add the toggle password feature using JavaScript.

Approach:

  1. Create an HTML form that contains an input field of type password. 
  2. Create a checkbox that will be responsible for toggling. 
  3. Create a function that will respond to toggling when a user clicks on the checkbox.

Example:

Password is neveropen. 
So, on typing it will show like this ************* 
And on clicking the checkbox it will show the characters: neveropen.

HTML




<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible"
          content="IE=edge">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0">
    <title>
        Show and hide password using JavaScript
    </title>
</head>
 
<body>
    <strong>
        <p>Click on the checkbox to show
            or hide password:
        </p>
    </strong>
 
    <strong>Password</strong>:
    <input type="password" value="neveropen"
              id="typepass">
 
    <input type="checkbox" onclick="Toggle()">
    <strong>Show Password</strong>
 
    <script>
        // Change the type of input to password or text
        function Toggle() {
            let temp = document.getElementById("typepass");
             
            if (temp.type === "password") {
                temp.type = "text";
            }
            else {
                temp.type = "password";
            }
        }
    </script>
</body>
</html>


Output:

Show and hide password using JavaScript

Show and hide passwords using JavaScript

RELATED ARTICLES

Most Popular

Dominic
32515 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6897 POSTS0 COMMENTS
Nicole Veronica
12013 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12109 POSTS0 COMMENTS
Shaida Kate Naidoo
7019 POSTS0 COMMENTS
Ted Musemwa
7262 POSTS0 COMMENTS
Thapelo Manthata
6976 POSTS0 COMMENTS
Umr Jansen
6964 POSTS0 COMMENTS