Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

81
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda