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

Last Updated : 04 Feb, 2025

Managing password visibility in web applications enhances user experience by allowing users to toggle between hidden and visible passwords. TypeScript provides strong type safety and better maintainability for implementing this feature.

What We Are Going to Create

**We’ll build an application that allows users to:

Project Preview

show-and-hide

Show And Hide Password Using TypeScript

Show And Hide Password- HTML and CSS Setup

This HTML code creates a simple password visibility toggle feature. It allows users to enter their password and toggle between showing and hiding it. This **CSS code provides styling for the password input field and toggle button, ensuring a clean and modern look.

HTML `

Enter Password

👁️

`

**In this example

Show And Hide Password - Typescript Logic

This TypeScript code handles the password visibility toggle. It switches the input field between text and password types when the user clicks the toggle button, ensuring a smooth and secure user experience.

JavaScript `

const inp = document.getElementById('password') as HTMLInputElement | null; const btn = document.getElementById('toggleVisibility') as HTMLButtonElement | null;

if (inp && btn) { btn.addEventListener('click', () => { if (inp.type === 'password') { inp.type = 'text'; btn.textContent = '🙈'; } else { inp.type = 'password'; btn.textContent = '👁️'; } }); }

`

**In this example

Convert to JavaScript File

Now You need to convert the TypeScript file into JavaScript to render by browser. Use one of the following command-

npx tsc task.ts
tsc task.ts

**Complete Code

HTML `

Enter Password

👁️

`