I am trying to create a log-in page and in it, I want there to be a button or check-box that will toggle the visibility of the password. The HTML is:
function showPassword(){
var pass = document.getElementById("Password");
if (pass.type === "password") {
pass.type = "text";
}
else {
pass.type = "password";
}
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://kit.fontawesome.com/ad6a81734d.js" crossorigin="anonymous"></script>
<link rel="stylesheet" href="~/css/custom.css"/>
</head>
<h1 id="login_header">Login</h1>
<div class="row">
<div class="col-md-12">
<form method="post">
<div class="text-danger validation-summary-valid" data-valmsg-summary="true"><ul><li style="display:none"></li>
</ul></div>
<div id ="log_reg" class="form-group">
<i class="fas fa-envelope" aria-hidden="true"></i>
<label for="Email">Email</label>
<input class="form-control" type="email" data-val="true" data-val-email="The Email field is not a valid e-mail address." data-val-required="The Email field is required." id="Email" name="Email" value="">
<span class="text-danger field-validation-valid" data-valmsg-for="Email" data-valmsg-replace="true"></span>
</div>
<div id ="log_reg"class="form-group">
<i class="fas fa-key" aria-hidden="true"></i>
<label for="Password">Password</label>
<input class="form-control" type="password" data-val="true" data-val-required="The Password field is required." id="Password" name="Password">
<span class="text-danger field-validation-valid" data-valmsg-for="Password" data-valmsg-replace="true"></span>
<input class="form-control" type="checkbox" data-val="false" id="Show Password" onclick="showPassword()" />
</div>
<div class="custom-checkbox">
<label for="RememberMe">
<input class="form-control" type="checkbox" data-val="true" data-val-required="The Remember me field is required." id="RememberMe" name="RememberMe" value="true">
Remember me
</label>
</div>
<button id="button1" type="submit" class="btn btn-primary">Login</button>
<input name="__RequestVerificationToken" type="hidden" value="CfDJ8OZRytqGjWNEjmm5lNWbbA4QbTKNuzp2dAWmUxUXfDlU4MdRo0VC9BJ7Irjjcx-3bsw-uBfYK-9723M-E4t0uFUUXglXGEGD7bAuWsxnMzHhX0hwOTX1p-bejSkH4ONUDjfhMOyBcMNpkmty_dYln1M"><input name="RememberMe" type="hidden" value="false"></form>
</div>
</div>
I have no idea what I am doing wrong here, but when I go to check the checkbox the password field remains the same and doesn't reveal itself. I have tried putting a script tag in the HTML file, but it doesn't do anything.
There is no 'toggle element' function that I know of, so I do it this way.
<!DOCTYPE html>
<html>
<body>
Password: <input type="password" value="password" id="myInput"><br><br>
<input type="checkbox" onclick="myFunction()">Show the Password
<script>
function myFunction() {
var pw_ele = document.getElementById("myInput");
if (pw_ele.type === "password") {
pw_ele.type = "text";
} else {
pw_ele.type = "password";
}
}
</script>
</body>
</html>
You can simply do like this
function togglePassword(){
var type = document.getElementById("Password").type;
if(type=='password'){
document.getElementById("Password").type = "text";
}else{
document.getElementById("Password").type = "password";
}
}
<input type="password" id="Password" name="Password">
<input class="form-control" type="checkbox" onclick="togglePassword()" />
function showPassword() {
const password = document.querySelector('#Password');
password.type = (password.type==="password") ? "text" : "password";
password.focus();
}
<form method="post">
<div class="text-danger validation-summary-valid" data-valmsg-summary="true">
<ul>
<li style="display:none"></li>
</ul>
</div>
<div id="log_reg" class="form-group">
<i class="fas fa-envelope" aria-hidden="true"></i>
<label for="Email">Email</label>
<input class="form-control" type="email" data-val="true" data-val-email="The Email field is not a valid e-mail address." data-val-required="The Email field is required." id="Email" name="Email" value="">
<span class="text-danger field-validation-valid" data-valmsg-for="Email" data-valmsg-replace="true"></span>
</div>
<div id="log_reg" class="form-group">
<i class="fas fa-key" aria-hidden="true"></i>
<label for="Password">Password</label>
<input class="form-control" type="password" data-val="true" data-val-required="The Password field is required." id="Password" name="Password">
<span class="text-danger field-validation-valid" data-valmsg-for="Password" data-valmsg-replace="true"></span>
<input class="form-control" type="checkbox" data-val="false" id="Show Password" onclick="showPassword()" />
</div>
<div class="custom-checkbox">
<label for="RememberMe">
<input class="form-control" type="checkbox" data-val="true" data-val-required="The Remember me field is required." id="RememberMe" name="RememberMe" value="true"> Remember me
</label>
</div>
<button id="button1" type="submit" class="btn btn-primary">Login</button>
<input name="__RequestVerificationToken" type="hidden" value="CfDJ8OZRytqGjWNEjmm5lNWbbA4QbTKNuzp2dAWmUxUXfDlU4MdRo0VC9BJ7Irjjcx-3bsw-uBfYK-9723M-E4t0uFUUXglXGEGD7bAuWsxnMzHhX0hwOTX1p-bejSkH4ONUDjfhMOyBcMNpkmty_dYln1M">
<input name="RememberMe" type="hidden" value="false">
</form>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With