JavaScript - Simple Toggle Password Using jQuery

Submitted by: 
Visitors have accessed this post 156 times.

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="<a href="">Sourcecodester</a>
  9. " rel="nofollow">">Sourcecodester</a>
  10. </a> </div>
  11. </nav>
  12. <div class="col-md-3"></div>
  13. <div class="col-md-6 well">
  14. <h3 class="text-primary">JavaScript - Simple Toggle Password Using jQuery</h3>
  15. <hr style="border-top:1px dotted #ccc;"/>
  16. <div class="col-md-3"></div>
  17. <div class="col-md-6" style="background-color:#ddd; border-radius:10px; padding:20px;">
  18. <h4>USER LOGIN</h4>
  19. <hr style="border-top:1px solid #000;"/>
  20. <div class="form-inline">
  21. <label>Username</label>
  22. <br />
  23. <input type="text" id="username" class="form-control">
  24. </div>
  25. <br />
  26. <div class="form-inline">
  27. <label>Password</label>
  28. <br />
  29. <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>
  30. </div>
  31. <br />
  32. <center><button class="btn btn-primary" id="login">Login</button></center>
  33. </div>
  34. </div>
  36. <script src="js/jquery-3.2.1.min.js"></script>
  37. <script src="js/script.js"></script>
  38. </body>
  39. </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!

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.