'How to show and hide password when click on eye icon using jquery
I have requirement to show and hide user password when click on eye icon so I had written script for that,when I click on eye icon only class is changing but password is not visible and again click on slash-eye icon it should hidden both these method not working how to solve this issue?
<input type="password" name="player_password" id="pass_log_id" />
<span toggle="#password-field" class="fa fa-fw fa-eye field_icon toggle-password"></span>
<script>
$("body").on('click','.toggle-password',function(){
$(this).toggleClass("fa-eye fa-eye-slash");
var input = $("#pass_log_id").attr("type");
if (input.attr("type") === "password") {
input.attr("type", "text");
} else {
input.attr("type", "password");
}
});
</script>
Solution 1:[1]
You have to remove var .attr("type"); from your var input = $("#pass_log_id").attr("type");.
You also can do it more elegant with ternary operator to toggle between type text and password:
$(document).on('click', '.toggle-password', function() {
$(this).toggleClass("fa-eye fa-eye-slash");
var input = $("#pass_log_id");
input.attr('type') === 'password' ? input.attr('type','text') : input.attr('type','password')
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.css" />
<body>
<input id="pass_log_id" type="password" name="pass" value="MySecretPass">
<span toggle="#password-field" class="fa fa-fw fa-eye field_icon toggle-password"></span>
</body>
Solution 2:[2]
<input type="checkbox" onclick="myFunction()">Show <input type="password" id="myInput" value="Password">
<script>
function myFunction() {
var x = document.getElementById("myInput");
if (x.type === "password") {
x.type = "text";
} else {
x.type = "password";
}
}
</script>
Solution 3:[3]
Please replace
var input = $("#pass_log_id").attr("type");
with
var input = $("#pass_log_id");
you need the element not the attribute,
Solution 4:[4]
HTML code
<input type="password" placeholder="Password" id="pwd" class="masked" name="password"
/>
<button type="button" onclick="showHide()" id="eye">
<img src="eye.png" alt="eye"/>
</button>
jquery code
$(document).ready(function () {
$("#eye").click(function () {
if ($("#password").attr("type") === "password") {
$("#password").attr("type", "text");
} else {
$("#password").attr("type", "password");
}
});
});
Solution 5:[5]
const togglePasswordEye = '<i class="fa fa-eye toggle-password-eye"></i>';
const togglePasswordEyeSlash = '<i class="fa fa-eye-slash toggle-password-eye"></i>';
$(togglePasswordEyeSlash).insertAfter('input[type=password]');
$('input[type=password]').addClass('hidden-pass-input')
$('body').on('click', '.toggle-password-eye', function (e) {
let password = $(this).prev('.hidden-pass-input');
if (password.attr('type') === 'password') {
password.attr('type', 'text');
$(this).addClass('fa-eye').removeClass('fa-eye-slash');
} else {
password.attr('type', 'password');
$(this).addClass('fa-eye-slash').removeClass('fa-eye');
}
})
.toggle-password-eye {
float: right;
top: -25px;
right: 10px;
position: relative;
cursor: pointer;
}
<link href="https://pro.fontawesome.com/releases/v5.10.0/css/all.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input id="password" type="password" class="form-control" name="password" required autocomplete="current-password">
Output:
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | adiga |
| Solution 3 | Akhil Punalur |
| Solution 4 | Sudaba Solaimankhil |
| Solution 5 | Talha Maqsood |


