How to Toggle Password Visibility in JavaScript17 Mar 2025 | 3 min read If you know how to set a password in a JavaScript code, it becomes easy to understand how we can toggle the visibility of the password. Here, in this section, we will learn how we can toggle the visibility of the password in JavaScript by implementing an example to do so. When we use a password field in the code, it might be well known that it is visible as '*', which is a security input for the user described password code. So, when the user inputs the password in the password field, it gets input in the form of '*' characters hiding the actual characters behind it. But sometimes, it does happen that the admin sometimes mistypes the password and assume that the password is correctly input. But after the unsuccessful login attempt, the admin wants to see the password string so that he can understand the mistake he is making and won't repeat it. For knowing the actual password string, he will move to the database to see the password. For such a reason, the concept of toggling the password visibility can help the admin see whether the admin has correctly entered the password or not there only, and there will be no need to move to the database and search for the password. We need to add a button that will let the user toggle the visibility of the password. Steps to Toggle the Password VisibilityIn order to toggle the visibility of the password, one needs to follow the below-described steps: Step 1: Create an input element with a password field that will take input in the password form. Step 2: Add an icon within the input element on which the user can click to toggle the visibility of the password. Step 3: Attach an event handler on making a click on the icon where if the user clicks on the icon, it should toggle the type attribute of the password field between text and password. Thus, when the type is changed between password to text, the actual text entered by the user will be visible. Step 4: So, when the user unclicks the icon, the actual password text again gets toggled in the password form. The user needs to include these described steps in the code and can gain the toggling of password visibility. Now, let's see an example code that will help us to better understand the steps. Toggling the Password Visibility in JavaScriptThe below example code shows the implementation of the steps required to toggle the visibility of the password: The output of the above code is shown below: ![]() In the code:
Therefore, using this method, we can toggle the visibility of the password. Next TopicRemoving Duplicate From Arrays |
CSV files are an essential part of computer science when you work with websites and databases. There might be some cases when the user has some data in the browser that you want to let them download this data. In that case, CSV files help...
6 min read
? A dynamic table is one whose number of rows varies based on the input it receives during runtime. Some websites or online programs, such as business websites, require the dynamic creation of a table while adding data or information. It can be done because JavaScript...
3 min read
JavaScript Date.now() Method JavaScript Date.now() function is used to display the time elapsed since January 1, 1970, 00:00:00 UTC. It will return the value in milliseconds. Since now() is a static method of the Date object, it will always be used as Date.now(). We cannot use...
2 min read
We often come across charts on analytics-based sites or some websites that contain surveys on some common agendas like population, percentage, and whatnot. These charts are often used in the Data Interpretation field to create network shares or profit distribution in core sectors. However, if we...
11 min read
When a user tries to load a page but it is unsuccessful, the page remains unloaded, and a JavaScript onunload event is triggered. The possibility exists that the onunload event will also happen if the browsers have been closed while the page is loaded. When a...
3 min read
? As we all know, the variable declaration is one of the basic and essential aspects of any programming language such as C language, C++, etc. However, JavaScript has a small odd, known as Hoisting, which can turn a flawless-looking declaration into a subtle bug. Hoisting In JavaScript,...
3 min read
The Element interface's insertAdjacentHTML() method converts the given text to HTML or XML and puts the generated nodes into the DOM tree at a predetermined point. To insert text as an HTML file at a certain point in the DOM, use the insertAdjacentHTML() method. The text...
4 min read
JavaScript code can be run independently in a browser using the open-source, cross-platform Node.js runtime environment. The "npm" represents "Node Package Manager". The Node.js package manager is used to install various internet packages as well as dependencies into a local environment using a command-line tool. It is...
3 min read
The onbeforeunload Event happens just before a document is unloaded. It displays a statement in a confirmation dialogue box to stay current page or go to the page using the provided link. You may ask the user whether they wish to stay on the...
4 min read
? JavaScript can be used to create a client-side image map. An image map is an image on the webpage that has multiple links to other pages. These links are called hotspots. An image map is used to navigate different links to other pages or on...
3 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India