Jquery show/hide password

半城伤御伤魂 提交于 2019-12-03 21:08:12

Here is one-of-the ways, how you can shorten your code:

$(document).ready(function() {

  $(".show-password, .hide-password").on('click', function() {
    var passwordId = $(this).parents('li:first').find('input').attr('id');
    if ($(this).hasClass('show-password')) {
      $("#" + passwordId).attr("type", "text");
      $(this).parent().find(".show-password").hide();
      $(this).parent().find(".hide-password").show();
    } else {
      $("#" + passwordId).attr("type", "password");
      $(this).parent().find(".hide-password").hide();
      $(this).parent().find(".show-password").show();
    }
  });
});
li {
  list-style: none
}

.hide-password {
  display: none
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul>
  <li>
    <input type="password" name="password" id="password" />
    <span class="password-showhide">
<span class="show-password">Show</span>
    <span class="hide-password">hide</span>
    </span>
    <li>
      <li>
        <input type="password" name="password" id="confirmPassword" />
        <span class="confirm-password-showhide">
<span class="show-password">Show</span>
        <span class="hide-password">hide</span>
        </span>
        <li>
</ul>

you can use only one button to control.

  • js:

```

$('.showOrHide').click(function(e){
    var target = e.currentTarget
    $(target).hasClass('show')?hidePassword($(target)):showPassword($(target))
})
function hidePassword(e){
    e.removeClass('show').addClass('hide')
    e.prev('input').attr('type','password')
}
function showPassword(e){
    e.removeClass('hide').addClass('show')
    e.prev('input').attr('type','text')
}

html:

```

<input type="text" value="123456">
<button class="showOrHide show">click</button>

```

style:

```

.show{
    /*css you want */
    color: blue
}
.hide{
    /*css you want */
    color: red
}

```

This is the shortest i think you can get

$(".confirm-password-showhide .trigger-password, .password-showhide .trigger-password").click(function() {
  var c = $(this).parent().attr("class").replace("-showhide", "");
  var obj = $("#" + (c.indexOf("confirm") > -1 ? "confirmPassword" : "password"));
  obj.attr("type", obj.attr("type") == "text" ? "password" : "text");
  $(this).text($(this).text() == "hide" ? "show" : "hide");
});

I've also removed the "hide" span from each of the passwords.

$(".confirm-password-showhide .trigger-password, .password-showhide .trigger-password").click(function() {
  var c = $(this).parent().attr("class").replace("-showhide", "");
  var obj = $("#" + (c.indexOf("confirm") > -1 ? "confirmPassword" : "password"));
  obj.attr("type", obj.attr("type") == "text" ? "password" : "text");
  $(this).text($(this).text() == "hide" ? "show" : "hide");
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul>
  <li><input type="password" name="password" id="password"> <span class="password-showhide"><span class="trigger-password">Show</span></span>
  </li>
  <li><input type="password" name="password" id="confirmPassword"> <span class="confirm-password-showhide"><span class="trigger-password">Show</span></span>
  </li>
</ul>

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>
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!