Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

82
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda