clock  Mon - Sun 8.00 AM - 8.00 PM
fb
instagram
play store
pinterest

How to Show and Hide Password using Eye Icon in HTML and JavaScript

writter  Sumit Dey Sarkar
Date  12 Nov 2022
Language  JavaScript
How to Show and Hide Password using Eye Icon in HTML and JavaScript

How to Show and Hide Password using Eye Icon in HTML and JavaScript

In this tutorial we will learn how to show and hide Password using eye icon in HTML and JavaScript.

Actually sometime we need to see password to determined password is right or wrong. So, here we use pure javascipt to toggle password visibility in forms using eye icon.

Let's see a example in which we can show/hide Password using JavaScript.

How to show and hide Password using eye icon in HTML and JavaScript?

Step 1 - Add below HTML

<html lang="en">
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">

<head>
  <title>How to show and hide Password using eye icon in HTML and JavaScript?</title>
</head>

<body>

  <form action="javascript:void(0)">
    <div class="form-div">
      <div class="input-div">
        <input type="password" class="form-control" placeholder="New Password">
        <div class="auth-left-icon">
          <img src="https://www.teknowize.com/attachments/file_1668193979.svg" alt="Mobile">
        </div>
        <img src="https://www.teknowize.com/attachments/file_1668193813.svg" alt="Mobile" class="inputEyeIcon">
      </div>
      <div class="buttonDiv"> <button type="submit" class="submitButton">Log in</button></div>
    </div>
  </form>

</body>

</html>

 

Step 2 - Add below CSS

    body {
      display: flex;
      justify-content: center;
      align-items: center;
    }

    .form-div {
      position: relative;
      width: 100%;
    }

    .input-div {
      position: relative;
      max-width: 370px;
      margin: 0 auto;
      margin-top: 1rem;
    }

    .input-div input {
      width: 100%;
      border: 1px solid #B8B8B8;
      border-radius: 5px;
      height: 50px;
      padding: 0 2.5rem 0 3.5rem;
      font-size: 18px;
      color: #333333;
    }

    .input-div input::placeholder {
      font-size: 18px;
      color: #B0B0B0;
    }

    .input-div .auth-left-icon {
      position: absolute;
      left: 0;
      top: 0;
      width: 50px;
      height: 50px;
      border-right: 1px solid #B8B8B8;
      border-radius: 5px;
      display: flex;
      justify-content: center;
      align-items: center;
    }

    .inputEyeIcon {
      position: absolute;
      top: 50%;
      right: 15px;
      transform: translateY(-50%);
      cursor: pointer;
    }

    .buttonDiv {
      display: flex;
      justify-content: center;
    }

    .form-div .submitButton {
      width: 250px;
      height: 50px;
      background: #000aff;
      border-radius: 8px;
      font-weight: 600;
      font-size: 20px;
      text-align: center;
      color: #FFFFFF;
      margin-top: 25px;
      border: 1.5px solid #004ff8;
      cursor: pointer;
    }

    .form-div .submitButton:hover {
      background: #ffffff;
      color: #000aff;
    }

 

Step 3 - Add below JavaScript

    // show hide password field
    var clearAllInputs = document.querySelectorAll('.inputEyeIcon');
    clearAllInputs.forEach(function (clearEachInputs) {
      clearEachInputs.addEventListener('click', function (e) {
        var inputAll = this.parentElement.querySelector('input');
        if (inputAll.type === "password") {
          inputAll.type = "text";
          this.src = "https://www.teknowize.com/attachments/file_1668193773.svg";
        } else {
          inputAll.type = "password";
          this.src = "https://www.teknowize.com/attachments/file_1668193813.svg";
        }
      })
    })

 

Now combine all the HTML, CSS and JS code together to show and hide password when click on eye icon using JavaScript.

<html lang="en">
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">

<head>
  <title>How to add modal popup in PHP using jQuery</title>
  <style type="text/css">
    body {
      display: flex;
      justify-content: center;
      align-items: center;
    }

    .form-div {
      position: relative;
      width: 100%;
    }

    .input-div {
      position: relative;
      max-width: 370px;
      margin: 0 auto;
      margin-top: 1rem;
    }

    .input-div input {
      width: 100%;
      border: 1px solid #B8B8B8;
      border-radius: 5px;
      height: 50px;
      padding: 0 2.5rem 0 3.5rem;
      font-size: 18px;
      color: #333333;
    }

    .input-div input::placeholder {
      font-size: 18px;
      color: #B0B0B0;
    }

    .input-div .auth-left-icon {
      position: absolute;
      left: 0;
      top: 0;
      width: 50px;
      height: 50px;
      border-right: 1px solid #B8B8B8;
      border-radius: 5px;
      display: flex;
      justify-content: center;
      align-items: center;
    }

    .inputEyeIcon {
      position: absolute;
      top: 50%;
      right: 15px;
      transform: translateY(-50%);
      cursor: pointer;
    }

    .buttonDiv {
      display: flex;
      justify-content: center;
    }

    .form-div .submitButton {
      width: 250px;
      height: 50px;
      background: #000aff;
      border-radius: 8px;
      font-weight: 600;
      font-size: 20px;
      text-align: center;
      color: #FFFFFF;
      margin-top: 25px;
      border: 1.5px solid #004ff8;
      cursor: pointer;
    }

    .form-div .submitButton:hover {
      background: #ffffff;
      color: #000aff;
    }
  </style>
</head>

<body>

  <form action="javascript:void(0)">
    <div class="form-div">
      <div class="input-div">
        <input type="password" class="form-control" placeholder="New Password">
        <div class="auth-left-icon">
          <img src="https://www.teknowize.com/attachments/file_1668193979.svg" alt="Mobile">
        </div>
        <img src="https://www.teknowize.com/attachments/file_1668193813.svg" alt="Mobile" class="inputEyeIcon">
      </div>
      <div class="buttonDiv"> <button type="submit" class="submitButton">Log in</button></div>
    </div>
  </form>

  <script type="text/javascript">

    // show hide password field
    var clearAllInputs = document.querySelectorAll('.inputEyeIcon');
    clearAllInputs.forEach(function (clearEachInputs) {
      clearEachInputs.addEventListener('click', function (e) {
        var inputAll = this.parentElement.querySelector('input');
        if (inputAll.type === "password") {
          inputAll.type = "text";
          this.src = "https://www.teknowize.com/attachments/file_1668193773.svg";
        } else {
          inputAll.type = "password";
          this.src = "https://www.teknowize.com/attachments/file_1668193813.svg";
        }
      })
    })
  </script>

</body>

</html>

 

Output

view output here- https://www.teknowize.com/snipits/login-form/show-and-hide-password-using-javascript

javascript

Comments 0

Leave a comment

Coursera, Codeacademy, Udacity, W3Schools, Udemy, Alison, TheNewBoston, edX, P.S.Codewars,Freecodecamp, Managing technical debt blog, Scrimba, Codepen, Codepen/challenges, The Odin Project, htmlreference.​io, cssreference.​io, Frontend Mentor, Dev Challenges, MDN, Code Mentor, Coding Dojo, CSS Battle, Codier, Ace Frontend, Can I Use, CSS Tricks, 30 Seconds of Code,tutorialspoint, Neumorphism, Shaddows Brumm, Fancy Border Radius, Glow Generator, Clothoid Corners, Glassmorphism, Clipy, CSS Filters, Base64 Image, Quantity Queries, Animations, Cubic-Bezier, Keyframes, Wait Animate, Transition.Style, graphic design, web design, website design, website builder, web developer, web designer, webdesign, ecommerce website, web design company, website creator, website designer, responsive web design, web development company, best website design, web design software, web page design, build a website, web developer salary, design website, web design courses, how to design a website, web design inspiration, website layout, web designer salary, web application development, ecommerce website design, web agency, software development company, web design tutorial, web programming, design company, website design templates, what is web designing, web developer jobs, website developer, web design agency, freelance web developer, web design services, freelance web designer, graphic design websites, web solutions, ecommerce website development, free website design, web development courses, webdev, web developers, web development tools, website design services, developpeur web, web design london, website design ideas, web designing and programming, design a website, web design and development, web dev, web development services, homepage design, best designed websites, cheap website design, learn web design, web design templates, web design tools, web design jobs, website design inspiration, web design india, flash website, website developers, designer websites, website services, website design cost, good website design, site design, simple website design, cool website designs, modern website design, graphic designer websites, webcode, best web design software, website making, free web design software, mobile website design, learn web development, front end web developer, how to become a web developer, web developer portfolio, web development company in india, python web development, web development tutorial, website company, website design and development, web company, webdesigning, professional website design, affordable web design, best web design company, creative web design, top website designs, website design pricing, web developer tools, how to develop a website