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

96
Visualizações
onclick multiple times javascript

So im making a form where theres an image on an eye that should show the password when clicked and when clicked again it should hide it. i made that when its clicked it change the src attribute of the img and change the type of input

if(img.src = 'images/eye1.png'){
        img.src = 'images/eye2.png';
        password.type = 'text'
}

and if the img.src is eye2 so after i click the first time it return back to the original

else if(img.src = 'images/eye2.png') {
        img.src = 'images/eye1.png';
        password.type = 'password'
    }

i tried with else too but it doesnt do anything after the first click and i cant find a solution. help please :(

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

The problem is that you are not verifying any equal condition since you have to use "==" (equal) and not "=" (assignment)

// This line
if(img.src == 'images/eye1.png'){
    img.src = 'images/eye2.png';
    password.type = 'text';

// and this one
} else if(img.src == 'images/eye2.png') {
    img.src = 'images/eye1.png';
    password.type = 'password';
}

It is possible to assign a value to a variable in an if. That's why you didn't see an error. I advise you to place the variable to the right side of the ==. Because when you try to assign a value to a text, the console will display an error, because the text is not a left value

Example:

if('images/eye2.png' == img.src) {
   // your code
}
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