Reputation: 1094
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>
Upvotes: 17
Views: 108704
Reputation: 113
function click() {
// toggle the type attribute
const togglePassword = document.querySelector("#togglePassword");
const passwordV = document.querySelector("#password_field");
const type = passwordV.getAttribute("type") === "password" ? "text" : "password";
togglePassword.className === 'fa fa-eye-slash viewpass mr-4 text-muted' ? document.getElementById("togglePassword").className = 'fa fa-eye viewpass mr-4 text-muted' : document.getElementById("togglePassword").className = 'fa fa-eye-slash viewpass mr-4 text-muted';
passwordV.setAttribute("type", type);
}
<input type="password" id="password_field"/>
<span className="fa fa-eye-slash viewpass mr-4 text-muted" onClick={click} id="togglePassword"></span>
In add CSS
.viewpass{
float: right;
margin-left: -25px;
margin-top: -26px;
position: relative;
font-size: large;
z-index: 2;
}
Upvotes: 0
Reputation: 1
This is simple script with handle password toggle Html and javascript code
<div class="input-box">
<input type="password" placeholder="Password" id="pass">
<img src="eye-close.png" onclick="handle()" id="eyeicon">
</div>
<script>
function handle() {
var pass = document.getElementById('pass')
var eyeicon = document.getElementById('eyeicon')
if (pass.type == "password") {
pass.type = "text"
eyeicon.src = "eye-open.png";
} else {
pass.type = "password"
eyeicon.src = "eye-close.png";
}
}
</script>
Upvotes: 0
Reputation: 307
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:
Upvotes: 2
Reputation: 300
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");
}
});
});
Upvotes: 1
Reputation: 1218
<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>
Upvotes: 5
Reputation: 31
Please replace
var input = $("#pass_log_id").attr("type");
with
var input = $("#pass_log_id");
you need the element not the attribute,
Upvotes: 1
Reputation: 4366
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>
Upvotes: 15
Reputation: 43479
Your input
is actually string. Check console, you should see that string does not have method attr()
because you assign $().attr()
to input
$("body").on('click', '.toggle-password', function() {
$(this).toggleClass("fa-eye fa-eye-slash");
var input = $("#pass_log_id");
if (input.attr("type") === "password") {
input.attr("type", "text");
} else {
input.attr("type", "password");
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<span toggle="#password-field" class="fa fa-fw fa-eye field_icon toggle-password">Show/Hide</span>
<input type="password" id="pass_log_id"/>
Upvotes: 25