Jquery show/hide password

前端 未结 7 744
失恋的感觉
失恋的感觉 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: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");
    });
    
    
    • Show
    • Show

提交回复
热议问题