In your Toggle function you are currently only updating the field with the id password. So when you change the type of that field, only that field is changed. You are not checking the other field called retypedpassword, therefor it won’t be changed. Check out the my addition to your code here below for a working version of your toggle for BOTH fields.
function Toggle() {
const fields = [password, retypedpassword]
fields.forEach( x => {
if (x.type === "password") {
x.type = "text";
} else {
x.type = "password";
}
});
}
And a second exmample using the style of your current code:
function Toggle() {
let btt = password
if (btt.type === "password") {
btt.type = "text";
} else {
btt.type = "password";
}
let otherfield = retypedpassword
if (otherfield.type === "password") {
otherfield.type = "text";
} else {
otherfield.type = "password";
}
}
Solution 2 :
Try this. Note I remove the toggle from the password field click and gave the fields a class
I also streamlined your submit handler and fixed spelling and added required
document.getElementById("showpasswords").addEventListener("click",function() {
[...document.querySelectorAll(".password")].forEach(p =>
p.type = p.type === "password" ? "text" : "password"
)
})
document.getElementById("registerdetails").addEventListener("submit", function(e) {
const errSpan = document.getElementById("passwordmatcherror");
let firstPassword = this.password.value;
let retypedPassword = this.retypedpassword.value;
errSpan.classList.toggle("error", firstPassword !== retypedPassword);
if (firstPassword !== retypedPassword) {
errSpan.innerHTML = ("Passwords do not match. Please retype");
e.preventDefault();
}
});
<section>
<div id="formcontainer">
<form id="registerdetails" action="fma_t3confirm.html">
<div>
<label for="password">* Password (Must be 8 characters and include one uppercase, one lowercase and
one numeric):</label><br/>
<input type="password" class="password" id="password" pattern="^(?=.*[a-z])(?=.*[A-Z])(?=.*d)[a-zA-Zd]{8,8}$" required>
<label><input type="checkbox" id="showpasswords"> Show passwords</label>
</div>
<div>
<label for="retypedpassword">* Retype your password:</label><br/>
<input type="password" class="password" id="retypedpassword">
<span id="passwordmatcherror"></span>
</div>
<input type="submit" />
</form>
</div>
</section>
Solution 3 :
A simple way is just adding another button for the second input:
function Toggle() {
let btt = password;
let btt2 = retypedpassword;
if (btt.type === "password") {
btt.type = "text";
btt2.type = "text";
} else {
btt.type = "password";
btt2.type = "password";
}
}
Solution 4 :
Add a class pwd in both password fields. Also remove the Toggle method call on click of 2nd password field
Your Toggle function:
In the Toggle function, get all the pwd fields and then toggle the type of all fileds.
function Toggle() {
let pwdFiels = document.querySelectorAll('.pwd');
for (let fields in pwdFiels) {
if (pwdFiels[fields].type === "password") {
pwdFiels[fields].type = "text";
} else {
pwdFiels[fields].type = "password";
}
}
}
Problem :
I am trying to find a solution if is possible to show / hide the password at the same time?
I could only show / hide for the first original password.
Does anybody know a solution for it or if is even possible?
Here is JavaScript and HTML code
function Toggle() {
let btt = password
if (btt.type === "password") {
btt.type = "text";
} else {
btt.type = "password";
}
}
function checkPassword(register) { //correct
let fisrtPassword = register.password.value;
let retypedPassword = register.retypedpassword.value;
if (fisrtPassword === retypedPassword) {
return true;
} else {
passwordMatchError.setAttribute("class", "error");
passwordMatchError.innerHTML = ("Passwords do not match. Please retype");
return false;
}
}
Comments
Comment posted by mplungjan
You mean the code that changes from password to text and back?
Comment posted by Lucas
Yes.. I’ve done the code to show the text of the original password, but I don’t know how to show the text of the second password that is perhaps matching with the original
Comment posted by Lucas
Ah because I was only using one type of the field it wasn’t working for the others. I see now. very useful. works for me. thank you.
Comment posted by Pim
I’m glad it helped you! Good luck with your coding!
Comment posted by Lucas
Perfect code. I enjoyed to see the different way to execute the code without having to use the Toggle from the password field. for some reason is not working, probably something in my code.. will check that out. I appreciate. really helpful.
Comment posted by Lucas
May I ask you. Do you know if is possible to add a required in JS? or is only in html
Comment posted by Lucas
Also great. I was almost close to your code. thanks to share your knowledge