Toggle Password Visibility using jQuery

This is the step by step procedure in this tutorial:

Add dependency

In the header portion of your HTML, add the following:
  1. <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
  2. <link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet">
  3. <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>

Add style

Include the following style.
  1. #myInput{
  2. font-size: 1em;
  3. padding: 1em;
  4. }

Add the Input

Next, we create our input type.
  1. <input type="password" id="myInput"> <button type="button" class="btn btn-primary" id="showBtn"><i class="fa fa-eye" id="icon"></i> <span id="btnText">Show</span></button>

Adding our jQuery Script

Last step is to add our jquery script.
  1. $(document).ready(function(){
  2. $('#showBtn').click(function(){
  3. var type = $('#myInput').attr('type');
  4. if(type == 'password'){
  5. $('#myInput').attr('type', 'text');
  6. $('#btnText').text('Hide');
  7. $('#showBtn').removeClass('btn-primary').addClass('btn-default');
  8. $('#icon').removeClass('fa-eye').addClass('fa-eye-slash');
  9. }
  10. else{
  11. $('#myInput').attr('type', 'password');
  12. $('#btnText').text('Show');
  13. $('#showBtn').removeClass('btn-default').addClass('btn-primary');
  14. $('#icon').removeClass('fa-eye-slash').addClass('fa-eye');
  15. }
  16. });
  17. });

Full HTML

  1. <!DOCTYPE html>
  2. <meta charset="utf-8">
  3. <title>Toggle Password Visibility using jQuery</title>
  4. <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
  5. <link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet">
  6. <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
  7. <style type="text/css">
  8. #myInput{
  9. font-size: 1em;
  10. padding: 1em;
  11. }
  12. </style>
  13. </head>
  14. <div class="container">
  15. <h1 class="page-header text-center">Toggle Password Visibility using jQuery</h1>
  16. <div class="row">
  17. <div class="col-sm-4 col-sm-offset-4">
  18. <input type="password" id="myInput"> <button type="button" class="btn btn-primary" id="showBtn"><i class="fa fa-eye" id="icon"></i> <span id="btnText">Show</span></button>
  19. </div>
  20. </div>
  21. </div>
  22. <script type="text/javascript">
  23. $(document).ready(function(){
  24. $('#showBtn').click(function(){
  25. var type = $('#myInput').attr('type');
  26. if(type == 'password'){
  27. $('#myInput').attr('type', 'text');
  28. $('#btnText').text('Hide');
  29. $('#showBtn').removeClass('btn-primary').addClass('btn-default');
  30. $('#icon').removeClass('fa-eye').addClass('fa-eye-slash');
  31. }
  32. else{
  33. $('#myInput').attr('type', 'password');
  34. $('#btnText').text('Show');
  35. $('#showBtn').removeClass('btn-default').addClass('btn-primary');
  36. $('#icon').removeClass('fa-eye-slash').addClass('fa-eye');
  37. }
  38. });
  39. });
  40. </body>
  41. </html>
That ends this tutorial. Happy Coding :)

Add new comment