Creating an Input Password Visibility Toggle (Show/Hide) using CSS and JavaScript Tutorial

In this tutorial, you can learn to create an Input Password Visibility Toggle (show/hide) using CSS and Pure JavaScript. The tutorial aims to provide students and beginners with a reference for learning to create some useful components such as the password visibility toggle for websites or web applications. Here, I will be providing a simple web page script that I created that demonstrates the creation of an Input Password Visibility Toggle.

What is Input Password Visibility Toggle?

The Input Password Visibility Toggle is an input option or button that triggers the password field value to show or hide. This feature allows the end-users to validate their entered password if it is right as they want. Stared or hidden input password field value might cause the user to save unintentional password value. Developers often implement this kind of component or feature aside from the confirmation input password to prevent human error/typo for saving passwords.

How to Create an Input Password Visibility Toggle using CSS and JS?

The Input Password Visibility Toggle can be easily achieved using CSS and JavaScript built-in properties, methods, and APIs. We can simply enclose the password in a div element so we can add the visibility toggle icon/button inside the input element. Then, the JavaScript built-in event listeners, method, and APIs can help us to make the visibility icon/button functional which is the main goal is to show the password value as readable or to hide the password. Check out the sample web page scripts that I created and provided below to understand it more and have an idea of how to achieve the goal of this tutorial.

Sample Web Page

The scripts below result in a simple web page that contains 2 password fields which are the Password and Confirm Password fields. Each password input has a visibility toggle located at the right side of the input element. Users can simply click the Icon to trigger show/hide the password value and update the toggle Icon. I am using Bootstrap v5 Framework and Google Icons on the scripts below where they are loaded using CDNs. Make sure to be connected to an internet connection to properly run the program.

Page Interface

Here is the HTML file script that is known as index.html. The file contains the page layout and password field elements.

  1. <!DOCTYPE html>
  2. <html lang="en">
  3.         <meta charset="UTF-8">
  4.         <meta http-equiv="X-UA-Compatible" content="IE=edge">
  5.         <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6.         <title>Toggle Password Field Show/Hide - HTML, CSS and JS</title>
  7.         <link rel="preconnect" href="https://fonts.googleapis.com">
  8.         <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
  9.         <link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Material+Symbols+Outlined:opsz,wght,FILL,GRAD@48,400,0,0" />
  10.         <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css">
  11.         <link rel="stylesheet" href="style.css">
  12.         <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>
  13. </head>
  14.         <div class="content-md-lg py-3">
  15.                 <div class="col-lg-8 col-md-10 col-sm-12 col-12 mx-auto">
  16.                         <div class="page-title">Toggle Password Field Show/Hide using HTML, CSS, and JavaScript</div>
  17.                         <hr style="margin:auto; width:25px" class="border-light opacity-100">
  18.                 </div>
  19.                 <!-- Sample Elements with Tooltips Wrapper -->
  20.                 <div class="col-lg-4 col-md-6 col-sm-8 col-12 mx-auto my-5">
  21.                         <div class="mb-3">
  22.                                 <label for="password" class="text-light fw-bolder">Password</label>
  23.                                 <div class="password-field">
  24.                                         <input type="password" id="password" class="form-control rounded-0">
  25.                                 </div>
  26.                         </div>
  27.                         <div class="mb-3">
  28.                                 <label for="confirm-password" class="text-light fw-bolder">Confirm Password</label>
  29.                                 <div class="password-field">
  30.                                         <input type="password" id="confirm-password" class="form-control rounded-0">
  31.                                 </div>
  32.                         </div>
  33.                 </div>
  34.                 <!-- Sample Elements with Tooltips Wrapper -->
  35.         </div>
  36.         <script src="script.js"></script>
  37. </body>
  38. </html>

Stylesheet

Here is the CSS file script that is known as style.css. This file contains the design codes of some elements of the page including the password fields and visibility toggle.

  1. @import url('https://fonts.googleapis.com/css2?family=Exo+2:ital,wght@0,100;0,200;0,300;0,400;0,500;0,600;0,700;0,800;0,900;1,100;1,200;1,300;1,400;1,500;1,600;1,700;1,800;1,900&display=swap');
  2.  
  3. *{
  4.         box-sizing: border-box;
  5.         font-family: 'Exo 2', sans-serif;
  6. }
  7. /**
  8. Page Design
  9. */
  10. body,
  11. html{
  12.         height: 100%;
  13.         width: 100%;
  14.         margin: 0;
  15.         padding: 0;
  16.         overflow-x:hidden;
  17. }
  18. body{
  19.         background-color: #282A3A;
  20. }
  21. .page-title{
  22.         font-size: 2.5rem;
  23.         font-weight: 500;
  24.         color: #fff;
  25.         letter-spacing: 3px;
  26.         font-family: var(--secular-font);
  27.         text-align: center;
  28.         text-shadow: 0px 0px 3px #2020208c;
  29. }
  30. .border-dark-subtle{
  31.         border-color: var(--border-dark-subtle) !important;
  32. }
  33.  
  34. /* Password Toggle Design */
  35.  
  36. /* Password Field */
  37. .password-field {
  38.         position: relative;
  39. }
  40.  
  41. /* Password Input */
  42. .password-field input#password {
  43.         padding-right: 50px;
  44. }
  45.  
  46. /* Password Visibility Button/Icon */
  47. .password-field span.password-icon {
  48.         position: absolute;
  49.         top: 0;
  50.         right: 0;
  51.         width: 50px;
  52.         height: 100%;
  53.         display: flex;
  54.         align-items: center;
  55.         justify-content: center;
  56.         cursor: pointer;
  57. }
  58. /* Password Visibility Icon Default Color */
  59. .password-field span.password-icon > span {
  60.         color: #b9b9b9;
  61. }
  62. /* Password Visibility Icon Hovered and Active Color */
  63. .password-field span.password-icon:hover > span,
  64. .password-field span.password-icon:active > span {
  65.         color: #242323;
  66. }

JavaScript

Lastly, here is the JavaScript file script that is known as script.js. This file contains the code that makes the password input visibility toggle functional.

  1. // Password Visible Icon
  2. const passwordShow = `<span class="material-symbols-outlined">visibility</span>`
  3. // Password Hidden Icon
  4. const passwordHide = `<span class="material-symbols-outlined">visibility_off</span>`
  5. /**
  6.         * Creating Visiblity Icon/Button Element
  7.         */
  8. const passwordVisiblityIcon = document.createElement('span')
  9.                 passwordVisiblityIcon.classList.add('password-icon')
  10.                 passwordVisiblityIcon.innerHTML = `${passwordHide}`;
  11.  
  12. //Selecting All Password Field
  13. const passwordEls = document.querySelectorAll('.password-field')
  14.  
  15. /**
  16.         * Function to execute when password visibility toggle has been clicked
  17.         * @param {dom} el - password field element
  18.         */
  19. const togglePasswordText = (el)=>{
  20.         // Check current password input type
  21.         var currentType = el.querySelector('input').type
  22.         if(currentType.toLowerCase() == 'password'){
  23.                 // update Password Icon to visible
  24.                 el.querySelector('.password-icon').innerHTML = passwordShow
  25.                 // Show Password as text
  26.                 el.querySelector('input').type = `text`
  27.         }else{
  28.                 // update Password Icon to hidden
  29.                 el.querySelector('.password-icon').innerHTML = passwordHide
  30.                 // Hide Password
  31.                 el.querySelector('input').type = `password`
  32.         }
  33.         // focus password input
  34.         el.querySelector('input').focus()
  35. }
  36.  
  37. /**
  38.         * Initialize Password Toggle Button
  39.         */
  40. passwordEls.forEach(el => {
  41.         // Cloning the Password Icon/Button Element
  42.         var _iconClone = passwordVisiblityIcon.cloneNode(true)
  43.         // Add Password icon to the passwrod field
  44.         el.appendChild(_iconClone)
  45.         // Event Listener to toggle Password Visibility
  46.         el.querySelector('.password-icon').addEventListener('click', e=>{
  47.                 e.preventDefault()
  48.                 // toggle password visibility
  49.                 togglePasswordText(el)
  50.         })
  51. })

Snapshots

Here are the snapshots of the overall result of the web page scripts I have provided above.

Page UI

Password Visibility Toggle (Show/Hide) using CSS and JavaScript

Input Password Visibility Show

Password Visibility Toggle (Show/Hide) using CSS and JavaScript

Input Password Visibility Hidden

Password Visibility Toggle (Show/Hide) using CSS and JavaScript

There you go! I have provide also the complete source code zip file of the web page script that I provided above on this website and it is free to download. The download button can be found after this tutorial's content. Feel free to download and modify it to do some experiments to enhance your programming capabilities.

There you go! I hope this Creating an Input Password Visibility Toggle (Show/Hide) using CSS and JavaScript Tutorial will help you with what you are looking for and will be useful for current and future web application projects.

Explore more on this website for more Tutorials and Free Source Codes.

Happy Coding =)

Add new comment