How to Toggle Password Visibility using HTML and JavaScript ? Last Updated : 29 Dec, 2023 Summarize Comments Improve Suggest changes Share Like Article Like Report In this article, we will learn about how to toggle password visibility using HTML and JavaScript. Passwords are those input types that take hidden inputs. It can be shown and hidden from the user by toggling its type between text and password. Approach The following approach will be implemented to toggle the visibility of the Password: The eye image (eye.png) will show the password to the user by changing the input type from password to text.The eye slash image (eyeslash.png) will hide the password from the user by adding the password type to the input.We will toggle the type of input field of the password from text -> password when clicking on the eye slash image and from password -> text on click to the eye image.Example: In this example, we will see the toggling of the password visibility using HTML and JavaScript. HTML <!DOCTYPE html> <html> <body> <center> <h2 style="color:green"> GeeksforGeeks </h2> <div> <form> Username <input type="text" name="username" autofocus="" autocapitalize="none" autocomplete="username" required="" id="id_username"> <br /><br /> Password <input type="password" name="password" autocomplete="current-password" required="" id="id_password"> <img src= "https://media.geeksforgeeks.org/wp-content/uploads/20210917145551/eye.png" width="1%" height="1%" style= "display: inline; margin-left: -1.5%; vertical-align: middle" id="togglePassword"> <br /><br /> <button type="submit"> Login </button> </form> </div> </center> <script> 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 if (togglePassword.src.match( "https://media.geeksforgeeks.org/wp-content/uploads/20210917150049/eyeslash.png")) { togglePassword.src = "https://media.geeksforgeeks.org/wp-content/uploads/20210917145551/eye.png"; } else { togglePassword.src = "https://media.geeksforgeeks.org/wp-content/uploads/20210917150049/eyeslash.png"; } }); </script> </body> </html> Output: Â Â Â Â Comment More infoAdvertise with us Next Article How to Toggle Password Visibility using HTML and JavaScript ? A akshitsaxenaa09 Follow Improve Article Tags : JavaScript Web Technologies HTML Blogathon Blogathon-2021 JavaScript-Methods HTML-Questions JavaScript-Questions +4 More Similar Reads How to Toggle Password Visibility in JavaScript ? In this article we will discuss how to toggle password visibility in JavaScript we will also use HTML and CSS in order to create a password visibility toggler for our webpage. Approach: The basic approach towards making password visibility toggler will be quite simple, we will use a button and write 4 min read Validate a password using HTML and JavaScript Validating a password using HTML and JavaScript involves ensuring that user-entered passwords meet certain criteria, such as length, complexity, or character types (e.g., uppercase, lowercase, numbers, and symbols). This process enhances security by enforcing strong password requirements before form 2 min read How to toggle password visibility in forms using Bootstrap-icons ? Toggle password visibility in forms allows users to reveal or hide their typed passwords for convenience and security. Using Bootstrap icons, you can implement this feature by toggling the input field's type attribute between "password" and "text" upon icon click, revealing or hiding the password. A 2 min read How to validate confirm password using JavaScript ? In almost every modern website we have seen a login and signup feature, which is surely an important service for both the client and the service provider. When a user signs up for a certain website they have to put in a username a password, to enter the password websites ask them to enter a password 3 min read How to Toggle Popup using JavaScript ? A popup is a graphical user interface element that appears on top of the current page, often used for notifications, alerts, or additional information. These are the following approaches to toggle a popup using JavaScript: Table of Content Using visibility propertyUsing ClassListUsing visibility pro 3 min read Like