How to Hide Password Dynamically using jQuery

How to Hide Password Dynamically using jQuery

Introduction

In this tutorial we will create a How to Hide Password Dynamically using jQuery. This tutorial purpose is to show how you can hide your password detail. This will cover all the basic function that will hide password. I will provide a sample program to show the actual coding of this tutorial.

This tutorial is simple and easy to understand just follow the instruction I provided and you can do it without a problem. This program can be use to any system if you want to hide your password from being visible. I will give my best to provide you the easiest way of creating this program Hide Password. So let's do the coding.

Before we get started:

First you have to download the jQuery plugin.

Here is the link for the jQuery that i used in this tutorial https://jquery.com/.

Lastly, this is the link for the template that i used for the layout design https://getbootstrap.com/.

Creating The Interface

This is where we will create a simple interface for our application. This code will html forms and a toggle button. To create this simply copy and write it into your text editor, then save it as index.html.
  1. <!DOCTYPE html>
  2. <head>
  3. <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  4. <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  5. </head>
  6. <nav class="navbar navbar-default">
  7. <div class="container-fluid">
  8. <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  9. </div>
  10. </nav>
  11. <div class="col-md-3"></div>
  12. <div class="col-md-6 well">
  13. <h3 class="text-primary">How to Hide Password Dynamically</h3>
  14. <hr style="border-top:1px dotted #ccc;"/>
  15. <div class="col-md-6" style="background-color:aqua; border-radius:10px; padding:20px;">
  16. <h4>MY PROFILE</h4>
  17. <div class="form-inline">
  18. <label>Username</label>
  19. <br />
  20. <input type="text" id="username" class="form-control" value="sourcecodester" readonly="readonly">
  21. </div>
  22. <br />
  23. <div class="form-inline">
  24. <label>Password</label>
  25. <br />
  26. <input type="password" id="password" value="ilovesourcecodester" class="form-control" readonly="readonly"> <button type="button" class="btn btn-primary" id="toggle"><span class="glyphicon glyphicon-eye-open" id="icon"></span> <span id="btnText">Show</span></button>
  27. </div>
  28. </div>
  29. </div>
  30.  
  31. <script src="js/jquery-3.2.1.min.js"></script>
  32. <script src="script.js"></script>
  33. </body>
  34. </html>

Creating JavaScript Function

This is where the main function of the application is. This code will hide your password dynamically when the button is clicked. To do this just copy and write these block of codes inside the text editor and save it as script.js.
  1. $(document).ready(function(){
  2. $('#toggle').on('click', function(){
  3. let pass=$('#password').attr('type');
  4. if(pass == 'password'){
  5. $('#password').attr('type', 'text');
  6. $('#btnText').text('Hide');
  7. $(this).removeClass('btn-primary').addClass('btn-success');
  8. $('#icon').removeClass('glyphicon-eye-open').addClass('glyphicon-eye-close');
  9. }
  10. else{
  11. $('#password').attr('type', 'password');
  12. $('#btnText').text('Show');
  13. $(this).removeClass('btn-success').addClass('btn-primary');
  14. $('#icon').removeClass('glyphicon-eye-close').addClass('glyphicon-eye-open');
  15. }
  16. });
  17.  
  18. });

In the given code we first call the basic jQuery ready event to signal that the DOM of the page is now ready to be use. Next we will bind the password textbox a specific variable called pass. In order for us to hide the password we will just use the attr() function. This will toggle the password attribute by changing the textbox type format from text to password or vice versa.

Output:

The How to Hide Password Dynamically using jQuery source code that I provide can be download below. Please kindly click the download button.

There you have it we successfully created How to Hide Password Dynamically using jQuery. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!

More Tutorials for JavaScript Language

JavaScript Tutorials

Comments

Submitted byOrwa Mohammed Omer (not verified)on Sun, 02/26/2023 - 21:18

Good!!~!

Add new comment