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

95
Views
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 answers
Answer question

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