Hide/Show Password using Eye icon in HTML and JavaScript
In this tutorial, I will learn how to hide and show password using eye icon in HTML and JavaScript. I implemented it for one of my projects as below.
This very useful feature you should have on your login form.
There are simple two steps to follow. First, create the HTML login form. Secondly, show/hide password when the user clicks on the eye icon.
Watch live demo
Don’t miss any of such tips…
Let’s see it step-by-step. I will walk you through the complete code.
1. Create HTML Login form with Eye Icon
We are going to place the eye icon inside the password text field. We can use the eye icon from the awesome font script library.
First of all import the awesome font Stylesheet into your HTML form page.
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.13.0/css/all.min.css">
Use the tag <i>
to display the eye icon. This icon is also known as the visibility eye icon.
<i class="far fa-eye" id="togglePassword"></i>
Use the below CSS to put the eye icon at the end of the password text field.
margin-left: -30px; cursor: pointer;
If you are new to CSS, refer to how to add CSS in HTML.
Put this tag below the passwords input field. This is how the sample form will look like with the password field.
<form method="post"> Username <input type="text" name="username" autofocus="" autocapitalize="none" autocomplete="username" required="" id="id_username"> Password <input type="password" name="password" autocomplete="current-password" required="" id="id_password"> <i class="far fa-eye" id="togglePassword" style="margin-left: -30px; cursor: pointer;"></i> <button type="submit" class="btn btn-primary">Login</button> </form>
2. Add JavaScript to Toggle Show/Hide Password
Copy and paste the below code into your JavaScript. This code is self-explanatory if you are familiar with the JavaScript basics.
const togglePassword = document.querySelector('#togglePassword'); const password = document.querySelector('#id_password'); togglePassword.addEventListener('click', function (e) { // toggle the type attribute const type = password.getAttribute('type') === 'password' ? 'text' : 'password'; password.setAttribute('type', type); // toggle the eye slash icon this.classList.toggle('fa-eye-slash'); });
You can also insert this code in your HTML page with <script>
tag.
Show/Hide Password in Django
In the sample picture shown above, I have implemented it for one of my websites developed using the Django Python framework. If you are developing it for Django, refer registration and login form in Django.
That’s all. Now you can hide and show password using eye icon in HTML with simple JavaScript and CSS code.
If you find any difficulties or need any customization, let me know in the comment section below.
Comments
Dakisemut
Does this work on WordPress?
Aniruddha Chaudhari
Yes. As it is pure JavaScript and HTML/CSS code, it will work on WordPress as well.
dilip
It is not working in Firefox. Is it any extra CSS we need to add for the input box or eye icon?
Mangal
bro, apni web site ko online kase kare?
Aniruddha Chaudhari
If you have a website running locally, you have to host it on the server to make it live.
Lamonier
Thank you so much!
Keep up with the good tutorials 🙂
Aniruddha Chaudhari
You’re welcome, Lamonier!
This keeps me motivated to share more. Happy weekend!
Ummehany Shanda
I am trying on bootstrap the eye icon is showing in the bottom of the input field.
How can I do that?
Aniruddha Chaudhari
You have to change your CSS accordingly.
elsha
Do you correct it? I have the same problem.
zJosh
Hi, Thanks for this! however I’m trying to get it to work on a form with two password fields, how can I do that? for example “type in new password” and “confirm new password”
Aniruddha Chaudhari
Here you go.
– Create two different input HTML tags for two password fields.
– You have to verify if both the passwords are the same. You can do this using JavaScript when the user clicks the submit button
ilyas
Hi! thanks for this. Is there a way to modify the js so that I can get two toggle-able password fields to work in the same form?
Aniruddha Chaudhari
In that case, write two different input tags for passwords with two different IDs (says id_password_1 and id_password_2). And then write two separate JS functions.
Rehana
Sir when I delete the auto increment
rows from myphp admin the otber line is missing the no. Like
1
2
4
3 is missing when i delete row 3 from
the table
Aniruddha Chaudhari
This is really difficult to analyze. It would be great if you can share your code snippet.
Sayani
This is working for edge but not for chrome, its coming as a square box
Aniruddha Chaudhari
This should not be the problem as I personally tested it on Google Chrome.
Lamar
Did you by any chance resolve this problem? because i’m struggling with this too.
Aniruddha Chaudhari
If it is not working for you, can you share the console log?
ARPITA DANGI
It is not working even if I copied it too. What’s the issue with it? Everything is correct only the eye icon is not visible.
Aniruddha Chaudhari
Check the source code of your webpage. If you are seeing the eye icon code, most probably the eye icon component is not well aligned. In that case, use proper padding and margin.
sada
Can we apply it in simple HTML or CSS without an eye icon pic?
Aniruddha Chaudhari
You can use the icon from font-awesome. Or if you want to avoid any icon, simply you can use any special character instead of an eye icon.
suneetha
can you explain the captcha concept in PHP?
Aniruddha Chaudhari
Sorry. I don’t have much experience working with PHP.
Hossein
Thanks for your perfect tutorial,
when I want to change the icon from FONTAWESOME, it doesn’t work.
like this one :
Do I need to change the Link?
thanks
Aniruddha Chaudhari
Looks like fontawesome resource file is not loading properly. Please check the console log for the exact reason. You might see some failure logs there.
Ashutosh
Nice work.
Aniruddha Chaudhari
Thanks! 🙂