How to Toggle Password Visibility using HTML and JavaScript ? Last Updated : 29 Dec, 2023 Comments Improve Suggest changes 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 Hide or show HTML elements using visibility property in JavaScript The visibility property is used to hide or show the content of HTML elements. The visibility property specifies that the element is currently visible on the page. The 'hidden' value can be used to hide the element. This hides the element but does not remove the space taken by the element, unlike the 1 min read How to Generate a Random Password using JavaScript? Creating a random password generator is an excellent beginner-friendly project that can be useful in real-world applications. Passwords are vital for securing accounts, and strong, randomized passwords enhance security significantly. How to Generate a Random Password using JavaScriptWhat We're Going 5 min read Show And Hide Password Using JavaScript When creating login or registration forms, a common feature is the ability to show and hide passwords. This improves user experience by allowing users to verify their input before submitting the form.What We're Going to CreateWe will create a "Password Hide and Show" feature that will allow users to 4 min read Password Validation Form Using JavaScript The password Validation form is used to check the password requirements such as the password must have at least one Uppercase, or lowercase, number, and the length of the password. We can also check this after submitting the form but it's not recommended. We can easily check before submitting the fo 4 min read Create a Password Validator using Bootstrap & JavaScript A password validator assesses password strength by verifying if it meets predetermined criteria like length, inclusion of uppercase and lowercase letters, digits, and symbols. It ensures robust security measures for user authentication and data protection. PrerequisitesHTMLBootstrapJavaScriptApproac 3 min read Like