A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/show-hide-password-using-javascript/ below:

Show And Hide Password Using JavaScript

Show And Hide Password Using JavaScript

Last Updated : 11 Jul, 2025

When creating login or registration forms, a common feature is the ability to show and hide passwords. This improves user experience by allowing users to verify their input before submitting the form.

What We're Going to Create

We will create a "Password Hide and Show" feature that will allow users to toggle the visibility of their password. It will include an input field for the password and a button or icon (like an eye) to show or hide the text. Using HTML for structure, CSS for styling, and JavaScript for functionality, clicking the button will toggle the input fieldโ€™s type between "password" (hidden) and "text" (visible), enhancing user experience and security.

Project Preview Show and hide password using JavaScript Show and Hide Password - HTML code

The "Show and Hide Password" feature allows users to toggle the visibility of their password input field with a button, enhancing usability and security in web forms.

HTML
<html>
<head></head>
<body>
    <div class="container">
        <label for="passes">Enter Password</label>
        <div class="input-container">
            <input id="passes" placeholder="Your password" type="password" />
            <span class="eye-icon" id="eye-icon"> ๐Ÿ‘๏ธ </span>
        </div>
    </div>
</body>
</html>

In this example

Show and Hide Password - CSS Styling

The CSS styling for the "Show and Hide Password" feature ensures a clean, user-friendly interface by styling the input field, button, and positioning the eye icon. It uses flexbox for alignment and adds padding, borders, and rounded corners for a polished look.

CSS
body {
    font-family: Arial, sans-serif;
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100vh;
    margin: 0;
    background-color: #f4f4f4;
}

.container {
    background-color: white;
    padding: 30px;
    border-radius: 10px;
    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
    width: 300px;
    text-align: center;
}

.input-container {
    display: flex;
    align-items: center;
    position: relative;
}

input {
    width: 100%;
    padding: 10px;
    margin: 10px 0;
    border: 2px solid #ccc;
    border-radius: 5px;
    font-size: 16px;
}

.eye-icon {
    position: absolute;
    right: 10px;
    cursor: pointer;
    font-size: 20px;
    color: #333;
}

label {
    font-size: 18px;
    margin-bottom: 10px;
    display: block;
}

In this example:

Show and Hide Password - JavaScript

The JavaScript functionality for the "Show and Hide Password" feature toggles the input field's type between "password" and "text" when the button is clicked, updating the button text accordingly. It ensures that the password visibility can be switched with each click, providing interactive user experience.

JavaScript
let input = document.getElementById("passes");
let btn = document.getElementById("btnss");
btn.addEventListener("click", function() {
    if (input.type === "password") {
        input.type = "text";
        btn.textContent = "Hide";
    }
    else {
        input.type = "password";
        btn.textContent = "Show";
    }
})

In this example

Complete code HTML
<html>
<head>
    <style>
        body {
            font-family: Arial, sans-serif;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            margin: 0;
            background-color: #f4f4f4;
        }
        .container {
            background-color: white;
            padding: 30px;
            border-radius: 10px;
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
            width: 300px;
            text-align: center;
        }
        .input-container {
            display: flex;
            align-items: center;
            position: relative;
        }
        input {
            width: 100%;
            padding: 10px;
            margin: 10px 0;
            border: 2px solid #ccc;
            border-radius: 5px;
            font-size: 16px;
        }
        .eye-icon {
            position: absolute;
            right: 10px;
            cursor: pointer;
            font-size: 20px;
            color: #333;
        }
        label {
            font-size: 18px;
            margin-bottom: 10px;
            display: block;
        }
    </style>
</head>
<body>
    <div class="container">
        <label for="passes">
            Enter Password
        </label>
        <div class="input-container">
            <input id="passes" placeholder="Your password" type="password" />
            <span class="eye-icon" id="eye-icon"> ๐Ÿ‘๏ธ </span>
        </div>
    </div>
    <script>
        let input = document.getElementById('passes');
        let eyeIcon = document.getElementById('eye-icon');
        eyeIcon.addEventListener('click', function () {
            if (input.type === 'password') {
                input.type = 'text';
                eyeIcon.textContent = '๐Ÿ™‰';
            } else {
                input.type = 'password';
                eyeIcon.textContent = '๐Ÿ‘๏ธ';
            }
        })
    </script>
</body>
</html>


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4