Show And Hide Password Using JavaScript (original) (raw)

Last Updated : 24 Jan, 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

Recording2024-12-31130827-ezgifcom-video-to-gif-converter

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 `

Enter Password
๐Ÿ‘๏ธ

`

**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 `

Enter Password
๐Ÿ‘๏ธ

`