I'm currently learning javascript. It works when I have one input, but when I have more than one I'm confused about looping through the icon and the input. What do you think is the problem?. Sorry for my bad english. I hope you understand what i talking about
const inputs = document.querySelectorAll('.show-hide-password');
const icon = document.querySelectorAll('i.password');
// Experiment 1
icon.forEach(function (ele) {
ele.addEventListener('click', function (e) {
const targetInput = e.target.previousElementSibling.getAttribute('type');
if (targetInput == 'password') {
targetInput.type = 'text';
ele.classList.remove('fa-eye-slash');
ele.classList.add('fa-eye');
} else if (targetInput == 'text') {
targetInput.type = 'password';
ele.classList.add('fa-eye-slash');
ele.classList.remove('fa-eye');
}
});
});
// Experiment 2
// icon.forEach(function (ele) {
// ele.addEventListener('click', function (e) {
// inputs.forEach(function (input) {
// const targetInput = input.getAttribute('type');
// if (targetInput == 'password') {
// targetInput.type = 'text';
// ele.classList.remove('fa-eye-slash');
// ele.classList.add('fa-eye');
// } else if (targetInput == 'text') {
// targetInput.type = 'password';
// ele.classList.add('fa-eye-slash');
// ele.classList.remove('fa-eye');
// }
// });
// });
//});
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css">
<div class="form-group">
<label>New Password</label>
<input type="password" class="form-control show-hide-password" autocomplete="off" required>
<i class="fa fa-eye-slash password"></i>
</div>
<div class="form-group">
<label>Confirm New Password</label>
<input type="password" class="form-control show-hide-password" autocomplete="off" required>
<i class="fa fa-eye-slash password"></i>
</div>
You have to change the element attribute with Element.setAttribute like this:
const inputs = document.querySelectorAll('.show-hide-password');
const icon = document.querySelectorAll('i.password');
// Experiment 1
icon.forEach(function (ele) {
ele.addEventListener('click', function (e) {
const targetInput = e.target.previousElementSibling.getAttribute('type');
if (targetInput == 'password') {
e.target.previousElementSibling.setAttribute('type', 'text');
ele.classList.remove('fa-eye-slash');
ele.classList.add('fa-eye');
} else if (targetInput == 'text') {
e.target.previousElementSibling.setAttribute('type', 'password');
ele.classList.add('fa-eye-slash');
ele.classList.remove('fa-eye');
}
});
});
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css">
<div class="form-group">
<label>New Password</label>
<input type="password" class="form-control show-hide-password" autocomplete="off" required>
<i class="fa fa-eye-slash password"></i>
</div>
<div class="form-group">
<label>Confirm New Password</label>
<input type="password" class="form-control show-hide-password" autocomplete="off" required>
<i class="fa fa-eye-slash password"></i>
</div>
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