Jquery show/hide password

前端 未结 7 742
失恋的感觉
失恋的感觉 2021-02-10 17:33

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/

相关标签:
7条回答
  • 2021-02-10 18:09

    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>

    0 讨论(0)
  • 2021-02-10 18:12

    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>

    0 讨论(0)
  • 2021-02-10 18:22
    <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>
    
    0 讨论(0)
  • 2021-02-10 18:24
    <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>
    
    0 讨论(0)
  • 2021-02-10 18:27

    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
    }
    

    ```

    0 讨论(0)
  • 2021-02-10 18:30

    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'));
          });
    });
    
    0 讨论(0)
提交回复
热议问题