How to Show/Hide i.e. Toggle Password visibility using jQuery and JavaScript?

Views: 474
Comments: 2
Like/Unlike: 2
Posted On: 27-Jan-2024 07:00 

Share:   fb twitter linkedin
beginer
Member
1328 Points
43 Posts


While filling up a password input field, there comes a situation where we want to see what we have typed till now. To see that, there is a checkbox click on which makes the characters visible. In this article we will see how to Hide/Show passwords using both JavaScript and jQuery.

Example

Html page=>

<!-- Password field -->
Password: <input type="password" value="FakeNicePSW" id="myPasswordInput">

<!-- An element to toggle between password visibility -->
<input type="checkbox" onclick="showPassword()">Show Password

JavaScript approach=>

function showPassword() {
   var x = document.getelementbyid("myPasswordInput");
   if (x.type === "password") {
       x.type = "text";
   } else {
       x.type = "password";
   }
}

jQuery approach=>

function showPassword() {
  var x = $("#myPasswordInput");
  if (x.attr("type") === "password") {
      x.prop("type", "text");
  } else {
      x.prop("type", "password");
  }
}

 

2 Comments

Great!


Raj
10-Feb-2024 at 05:16

great!


Smith
20-Mar-2024 at 21:59
 Log In to Chat