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.

Image showing a hidden password.
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.

Image showing the previous hidden password as visible.
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 style sheet.

Image showing how to link a webpage to a stylesheet.
We link our webpage to a style sheet.

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

Cod example showing an input wrapper.
Add a div that serves as an input wrapper. Both the input and the icon are placed inside it.

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.

Code example showing a placeholder attribute in the input element.
We have added a placeholder attribute inside the input password.

Adding Styles

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

Image showing how browser renders an input field by default.
We see an input field on the left top corner of our browser.

Let’s add some styles to make it presentable.

Code example of styles adden in the same input field.
Add CSS to style our webpage.

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

Code that adds 300px maring to both top and bottom of the input box.

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

Code centering an input box using flexbox.
Center elements inside the input-box with Flexbox.

Example of the input-box without flexbox:

Image showing how input looks without flexbox.
How input looks without flexbox.

And this is how our input is end.

Image showing input box elements being aligned.
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.

Code showing JavaScript variables.
Create two variables for easy element selection.

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.

Code to make eye icon triggering the fuction.

We use console to check if onclick is working.

Image showing how console.log checks if onclick works.
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.

Code for toggling input type.
Conditional statement checks the type of input and change it back and forth.

We also need to make the icon to change accordingly.

Code that makes icons change accordingly to type.
We make icons change accordingly to type.

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

Gif showing the password visibility toggler in action.
Our password visibility toggler in action!

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