Jquery show/hide password

Viewed 28050

Below Jquery is working fine but How could I shorten this Jquery? I have to show and hide the password for password and confirmed password? https://jsfiddle.net/c5cvLo54/1/

$(".password-showhide .show-password").click(function() {
    $("#password").attr("type", "text");
    $(".password-showhide .show-password").hide();
    $(".password-showhide .hide-password").show();
});
$(".password-showhide .hide-password").click(function() {
    $("#password").attr("type", "password");
    $(".password-showhide .hide-password").hide();
    $(".password-showhide .show-password").show();
});

$(".confirm-password-showhide .show-password").click(function() {
    $("#confirmPassword").attr("type", "text");
    $(".confirm-password-showhide .show-password").hide();
    $(".confirm-password-showhide .hide-password").show();
});
$(".confirm-password-showhide .hide-password").click(function() {
    $("#confirmPassword").attr("type", "password");
    $(".confirm-password-showhide .hide-password").hide();
    $(".confirm-password-showhide .show-password").show();
});
8 Answers

I have created this basically by taking reference from Google's show password toggle on their login forms.

This basically checks whether a check box is checked or not.

If checked then it changes the input type of password to text and vice versa.

<!-- Toggle password visibility -->

    $(document).ready(function() {
        $("#show-password").change(function(){
            $(this).prop("checked") ?  $("#upassword").prop("type", "text") : $("#upassword").prop("type", "password");    
        });
    });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<input type="password" name="password" id="upassword" />
<input type="checkbox" id="show-password">
  <label for="show-password">Show Password

show/Hide password function

function togglePassword($element){
    var newtype=$element.prop('type')=='password'?'text':'password';
    $element.prop('type',newtype);
}
$(document).ready(function() {
      $('#showUserNewPass').change(function(){
          togglePassword($('#userNewPass'));
          togglePassword($('#confirmNewPassword'));
      });
});
<html>
    <head>
    <title>This Password Hide & Show</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"> 
   </script>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
    <style>
        #password
        {
            width: 50%;

        }
        #open
        {
            color: gray;
        }
        #closed
        {
            display: none;
            color: steelblue;
        }
        i
        {
            position: absolute;
            margin-top: -30px;
            margin-left: 280px;
        }
    </style>
     <script type="text/javascript">
         $(document).ready(function(){

             $("#open").click(function(){
                 $("#open").hide();
                 $("#closed").show();
                 $("#password").attr("type","text");
             });

             $("#closed").click(function(){
                 $("#closed").hide();
                 $("#open").show();
                 $("#password").attr("type","password");
             });

         });

</script>
</head>
<body>
    <h4>Password Hide & Show</h4>
    <input class="form-control" type="password" placeholder="Password" id="password" name="pwd">
    <i id="open" class="fa fa-eye fa-2x"></i>
    <i id="closed" class="fa fa-eye-slash fa-2x"></i>
</body>

</html>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">  
  <div class="container">
        <div class="row">
          <div class="col-md-offset-3 col-md-6">
            <h2>Show or Hide Password</h2>
            <p class="instructions">Hover on the eye to show/hide the password</p>
            <div class="input-group">
              <input id="password" type="password" class="form-control" placeholder="password">
              <span class="input-group-btn">
                <button id="show_password" class="btn btn-secondary" type="button">
                  <span class="glyphicon glyphicon-eye-open"></span>
                </button>
              </span>
            </div>
          </div>
        </div>
      </div>
    <script>
        $("#show_password").hover(
          function functionName() {
            //Change the attribute to text
            $("#password").attr("type", "text");
            $(".glyphicon").removeClass("glyphicon-eye-open").addClass("glyphicon-eye-close");
          },
          function () {
            //Change the attribute back to password
            $("#password").attr("type", "password");
            $(".glyphicon").removeClass("glyphicon-eye-close").addClass("glyphicon-eye-open");
          }
        );
      </script>
<script>
        
        $(document).ready(function(){

            $('#btn').click(function(){

                var paswd= $('#password');
                if(paswd.attr("type")== "password"){

                    paswd.attr("type","text");

                    $('#btn').attr("value","hide");
                    
                }
                else{
                        paswd.attr("type","password");

                        $('#btn').attr("value","show");
                }
            })
        });
    
       
    </script>
Related