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

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

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 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