JavaScript Tutorial: How to Implement a Password Visibility Toggle

JavaScript Tutorial: How to Implement a Password Visibility Toggle

Greetings and welcome to another thrilling tutorial presented by stakedesigner Constructing projects is considered to be one of the most effective methods of acquiring knowledge in Javascript.

JavaScript is a powerful and versatile programming language that is widely used for creating interactive web pages and web applications. One of the many things that can be achieved with JavaScript is implementing a password visibility toggle, which allows users to show or hide the characters in a password field as they type.

In this JavaScript tutorial, How to Implement a Password Visibility Toggle we will show you how to implement a password visibility toggle using simple and easy-to-follow steps. We will start by discussing the basic concepts behind password visibility toggles and the benefits they offer to users. We will then move on to the actual implementation, where we will use HTML, CSS, and JavaScript to create a password field with a visibility toggle button.

This tutorial is designed for developers who have some basic knowledge of HTML, CSS, and JavaScript. However, even if you are a complete beginner, you should be able to follow along with the tutorial and learn how to implement a password visibility toggle in your web applications.

By the end of this tutorial, you will have a clear understanding of how password visibility toggles work and how to implement them in your own web applications. You will also have gained some valuable experience working with HTML, CSS, and JavaScript, which you can apply to other web development projects in the future. So, let’s get started!

Video JavaScript Tutorial :

To enhance your comprehension of how we developed the functionality of this project, I recommend watching the video provided below. If you find the video informative, please give it a like and subscribe to my YouTube channel, where I share new tips, tricks, and tutorials pertaining to HTML, CSS, and Javascript.

Folder Structure for the JavaScript Tutorial Password Visibility Toggle Project

Prior to commencing the coding process, let’s establish the folder structure for the project. We will create a folder for the project named ‘Currency Converter.’ Within this folder, we will have files for index.html, style.css, script.js, as well as SVG icon files.

HTML

Let’s start by creating the HTML code for JavaScript Tutorial. Begin by creating a file named ‘index.html.’ Then, copy the code below and paste it into the newly created HTML file.

<!DOCTYPE html>
<html>
  <head>
    <title>www.stakedesigner.com</title>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <link
      rel="icon"
      href="assets/img/favicon.png"
      sizes="32x32"
      type="image/png"
    />
    <link rel="stylesheet" href="assets/css/style.css" />
  </head>
  <body>
    <div class="password-block">
      <input id="password" value="Subscribe" type="password" />
      <span id="toggleBtn" onclick="toggle()"> show </span>
    </div>
  </body>
</html>

CSS

Afterward, we’ll style our list using CSS. To do so, copy the code provided below and paste it into your stylesheet.

body {
  background-color: #ffff;
  height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
}


.password-block {
  display:flex;
  border: solid 1px #f2f2f2;
  max-width: fit-content;
  padding: 4px 10px;
  position: relative;
}

.password-block input {
  border: none;
}

.password-block input:focus {
  outline: none;
}

.password-block #toggleBtn {
  background-color: #000;
  color: #fff;
  padding-left: 10px;
  padding-right: 10px;
  cursor: pointer;
  font-size: 13px;
  min-width: 60px;
  line-height: normal;
  display: flex;
  align-items: center;
  justify-content: center;
  height: 30px;
}

JavaScript

Lastly, we will incorporate functionality using Javascript. Once again, copy the code below and paste it into your script file.

 
      function toggle() {
        const passwordInput = document.getElementById("password");
        passwordInput.type = passwordInput.type === "password" ? "text" : "password";
        document.getElementById("toggleBtn").innerHTML = passwordInput.type === "password" ? "show" : "hide";
      }
  

function named toggle which will be used to toggle the visibility of a password input field. getElementById method and assigns it to a constant variable passwordInput. The getElementById method takes an argument, which is the ID attribute of the HTML element that you want to retrieve. password input field between “text” and “password”. The type property of an input field determines the type of input it accepts. Here, the ternary operator is used to check if the current type of the password input field is “password”. If it is, then it sets the type to “text”, and if it is not, it sets the type to “password”. This effectively toggles the visibility of the password.

changes the innerHTML of an element with the ID “toggleBtn” based on the current type of the password input field. If the current type is “password”, it sets the innerHTML to “show”, and if it is not, it sets the innerHTML to “hide”. This updates the text on the button to indicate whether the password is currently visible or hidden.

Overall, this code creates a function that toggles the visibility of a password input field and updates the text on a button to indicate whether the password is currently visible or hidden. It does this by changing the type property of the password input field and the innerHTML property of a button element.

3 thoughts on “JavaScript Tutorial: How to Implement a Password Visibility Toggle

Leave a Reply