Skip to main content

JavaScript - Simple Toggle Password Using jQuery

In this tutorial we will create a Simple Toggle Password using jQuery. This code can toggle the input password visibility when the user click the button. The code itself use jQuery onclick function to manipulate the textbox attribute by changing the type from password to text. This is a user-friendly program feel free to modify it.

We will be using jQuery a JavaScript framework that design to simplify HTML DOM tree traversal and manipulation. It can do more in a single line of code than JavaScript can do in a whole function.

Getting Started:

This is the link for the bootstrap that i used for the layout design

And this is the link for the jquery that i used in this tutorial

The Main Interface

This code contains the interface of the application. To create this just write these block of code inside the text editor and save this 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="">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">JavaScript - Simple Toggle Password Using jQuery</h3>
  14. <hr style="border-top:1px dotted #ccc;"/>
  15. <div class="col-md-3"></div>
  16. <div class="col-md-6" style="background-color:#ddd; border-radius:10px; padding:20px;">
  17. <h4>USER LOGIN</h4>
  18. <hr style="border-top:1px solid #000;"/>
  19. <div class="form-inline">
  20. <label>Username</label>
  21. <br />
  22. <input type="text" id="username" class="form-control">
  23. </div>
  24. <br />
  25. <div class="form-inline">
  26. <label>Password</label>
  27. <br />
  28. <input type="password" id="password" class="form-control"> <button type="button" class="btn btn-primary" id="toggle"><span class="glyphicon glyphicon-eye-open" id="icon"></span> <span id="btnText">Show</span></button>
  29. </div>
  30. <br />
  31. <center><button class="btn btn-primary" id="login">Login</button></center>
  32. </div>
  33. </div>
  35. <script src="js/jquery-3.2.1.min.js"></script>
  36. <script src="js/script.js"></script>
  37. </body>
  38. </html>

Creating the Script

This code contains the script of the application. This code will hide the password when the button is clicked. To do this just copy and write these block of codes as shown below inside the text editor and save it as script.js inside the js directory

  1. $(document).ready(function(){
  2. $('#toggle').on('click', function(){
  3. var pass=$('#password').attr('type');
  4. if(pass == 'password'){
  5. $('#password').attr('type', 'text');
  6. $('#btnText').text('Hide');
  7. $(this).removeClass('btn-primary').addClass('btn-default');
  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-default').addClass('btn-primary');
  14. $('#icon').removeClass('glyphicon-eye-close').addClass('glyphicon-eye-open');
  15. }
  16. });
  18. $('#login').on('click', function(){
  19. var username=$('#username').val();
  20. var password=$('#password').val();
  22. if(username=="" || password==""){
  23. alert("Please enter something");
  24. }else{
  25. alert('Your username is '+username+' and password is '+password);
  26. }
  28. });
  29. });

There you have it we successfully created a Simple Toggle Password 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!

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.


1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Add new comment

This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.