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();
});
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>
来源:https://stackoverflow.com/questions/45561636/jquery-show-hide-password