How to toggle password visibility using JavaScript. Easy implementation

In this post, we are going to develop a password input field with the option to hide or display the password.

This is how the password input field is going to look like with the password hidden.

By clicking the eye-slash icon password becomes visible.

Password is hidden. We have the option to make it visible by clicking the eye-slash icon.

And this is how it is going to look after we have clicked the eye-slash icon.

Clicking the eye-slash icon makes the password visible.

In order to create this simple tutorial, we are going to need two icons.

I have used two icons from the icons8 website.

You can download them using the below links.

The eye icon, and the eye slash icon.

The links are free, provided that we give the appropriate attribution to the creators.

Creating the password input with HTML

We create the html file, name it test.html, and then we add some boilerplate code.

We then create a second file, name it main.css. We are going to use this file as our stylesheet.

We link our webpage to a stylesheet.
(click on the image to open in a new tab)

Then we add a div that serves as an input wrapper. Let’s call it input-box.

Add a div that serves as an input wrapper. Both the input and the icon are placed inside it.
(click on the image to open in a new tab)

Inside that div we add the password input field with a placeholder of “password” and an image.

Notice that we have also added two IDs that we are going to use later in our JS code.

We have added a placeholder attribute inside the input password.

(click on the image to open in a new tab)

Adding Styles

Opening this file with the browser of our choice we get an input field on the left top corner.

We see an input field on the left top corner of our browser.

Let’s add some styles to make it presentable.

Add CSS to style our webpage.
(click on the image to open in a new tab)

Here we use margin to place the input-box near the center of the viewport.

(click on the image to open in a new tab)

We use flex to align the input field with the eye icon and center them inside the input-box div.

Center elements inside the input-box with Flexbox.
(click on the image to open in a new tab)

Example of the input-box without flexbox:

How input looks without flexbox.

And this is how our input is end.

Elements are now aligned.

Adding JavaScript

The script tag in HTML serves as a wrapper for embedding JavaScript code into an HTML document.

Inside it, we are going to develop our JavaScript code so we can toggle password visibility.

First we are going to create two variables that are going to make the selection of html elements easy.

Create two variables for easy element selection.
(click on the image to open in a new tab)

We then going to create a function that will make the password visibility toggling possible.

We are going to use the icon to trigger the function.

(click on the image to open in a new tab)

We use console to check if onclick is working.

Onclick works!

We then add a conditional statement that checks the type of input and alter it accordingly.

If the password is hidden (type=’password’) it makes it visible (type=’text’), and vice versa.

Conditional statement checks the type of input and change it back and forth.
(click on the image to open in a new tab)

We also need to make the icon to change accordingly.

We make icons change accordingly to type.
(click on the image to open in a new tab)

And now we can toggle password visibility in our password input field by clicking the eye icon.

Our password visibility toggler in action!

Although my blog doesn’t support comments, feel free to reply via email or X.

Privacy Policy Lambros Hatzinikolaou © 2024 — Today. All rights reserved.