Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

80
Views
Show hide password

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>

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!