Show/hide Password Eye Icon HTML – Eye Visibility JavaScript

Password Eye Icon Visibility JavaScript
Code Snippet:Show Hide Password Field on icon click (Native JS)
Author: Wellington Roth
Published: January 16, 2024
Last Updated: January 22, 2024
Downloads: 18,200
License: MIT
Edit Code online: View on CodePen
Read More

This Vanilla JavaScript code snippet helps you to create a password visibility eye icon toggle button to show/hide passwords. It gets the password input field and changes its type from password to text to show the entered password. The snippet uses Font Awesome eye icon inside the password input field and Bootstrap CSS for basic input design. However, you can integrate it with Bootstrap 4/5 or with your custom-designed login/signup form.

How to Create Password Eye Icon Visibility JavaScript

First of all, load Bootstrap (optional) and Font Awesome CSS into the head tag of your HTML document.

<link rel="stylesheet" href="" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<link rel="stylesheet" href="">

After that, create the HTML password input with a unique id “passInput”. Create a span element just after the input with an id “passBtn” and place Font Awesome eye icon inside it.

<div class="form-wrapper">
  <form id="form_id" class="form-class">
    <p class="input-group">
    <input id="passInput" class="form-control" placeholder="Your Password" name="password" type="password" size="30" aria-required="false">
    <span class="input-group-addon" role="button" title="veiw password" id="passBtn">
      <i class="fa fa-eye fa-fw" aria-hidden="true"></i>

If you are using Bootstrap, you don’t need any additional CSS styles.

.form-wrapper {
  margin: 10rem auto;
  max-width: 30rem;

Finally, add the following JavaScript function to toggle the password visibility on the click event.

const PassBtn = document.querySelector('#passBtn');
PassBtn.addEventListener('click', () => {
    const input = document.querySelector('#passInput');
    input.getAttribute('type') === 'password' ? input.setAttribute('type', 'text') : input.setAttribute('type', 'password');

   if (input.getAttribute('type') === 'text'){
     PassBtn.innerHTML = '<i class="fa fa-eye-slash"></i>';
  } else{
     PassBtn.innerHTML = '<i class="fa fa-eye fa-fw" aria-hidden="true"></i>';


That’s all! hopefully, you have successfully created a password visibility toggle using JavaScript. If you have any questions or suggestions, feel free to comment below.

Connect with us on social media:

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

About CodeHim

Free Web Design Code & Scripts - CodeHim is one of the BEST developer websites that provide web designers and developers with a simple way to preview and download a variety of free code & scripts. All codes published on CodeHim are open source, distributed under OSD-compliant license which grants all the rights to use, study, change and share the software in modified and unmodified form. Before publishing, we test and review each code snippet to avoid errors, but we cannot warrant the full correctness of all content. All trademarks, trade names, logos, and icons are the property of their respective owners... find out more...

Please Rel0ad/PressF5 this page if you can't click the download/preview link