Easy way to show/hide password for javascript beginners.
Tags: javascript, html, beginners, tutorial
Published: 2020-05-30 12:15:51

---
title: Easy way to show/hide password for javascript beginners.
published: true
description: Easy script to show or hide password on a form
tags: javascript, html, beginners, tutorial
cover_image: https://i.imgur.com/Obb72lw.jpg
---

<meta name="monetization" content="$ilp.uphold.com/7QaxJkDwRZED">

<mark> <strong>
You'll see a password input with a nice appearance and behaviour on the codepen linked at the bottom, as well this simple example. 
If you want or need or like a form styling tutorial let a comment and I'll take it in mind! =D
</strong> </mark>

First things first: 

<h2>Structure: </h2>
```html
<label for="passwd">Password</label>
  <div class="relative">
  <input type="password" id="passwd" name="passwd" />
  <i class="fa fa-eye showpwd" onClick="showPwd('passwd', this)"></i>
</div>
```

<small> I learnt how to put html code for dev.to not rendering it... claps on me!</small>

You can see the onClick attribute on the icon, it calls the script below with the target input ID and this as param (by context, <code>this</code> here is the main &lt;i> element that triggers the onClick event).

So on the script we'll receive a String and an Object.

<h2>basic styling:</h2>

```javascript
.relative {
  position: relative;
  width: fit-content;
  i {
    position: absolute;
    top: 3px;
    right: 5px;
  }
}
```


<h2>Script:</h2>

```javascript
function showPwd(id, el) {
  let x = document.getElementById(id);
  if (x.type === "password") {
    x.type = "text";
    el.className = 'fa fa-eye-slash showpwd';
  } else {
    x.type = "password";
    el.className = 'fa fa-eye showpwd';
  }
}
```

We are selecting an element by the first received param which must match to an input element.
If this DOM element match with the type password, simply change it's type to text. 
Changing the type of the input will make the content readable. 
We change the icon class too, adding a slash for giving feedback to the user.

If the type of the input isn't password means that it's already shown and the user clicked on the non-slashed eye icon, so we set the type to password again to hide the characters on it, and set the icon back to the original position.

You're done!


{% codepen https://codepen.io/joelbonetr/pen/ZEbgQVG %}


<h2> Different approach, please? Of course!</h2>


You can also attach an eventlistener instead using onclick and it will look something like:

```javascript
window.onload = function () {


  const elements = document.querySelectorAll('[type="password"]');
  
  elements.forEach(function(elem) {
    elem.parentNode.querySelector('i').addEventListener('click', function(){
      if (elem.type === "password") {
        elem.type = "text";
        this.className = 'fa fa-eye-slash showpwd';
      } else {
        elem.type = "password";
        this.className = 'fa fa-eye showpwd';
      }
    });
  });


}
```

We wait for window to load but you can use document onload instead if you want. The difference is that document.onload will wait till DOM loads and window.onload will wait till DOM plus all linked scripts and style sheets. Use one or another but you'll need this little waiting because if you try to add an eventlistener (or doing whatever) on a DOM element that does not exists yet, you'll get an error and the javascript execution will stop.

Then we search for all elements with type="password" on the DOM.
For each found element we search for the icon next to it, that must be wrapped inside same parent element to work "as is".

<mark>The literal expression is something like: "Select the &lt;i> element inside the parent element of my element with type="password".</mark>

Then on this icons we set a click event listener which triggers the same function explained avobe.

{% codepen https://codepen.io/joelbonetr/pen/bGVXrox %}


<hr>

As promised and as almost always here is the <a target="_blank" href="https://codepen.io/joelbonetr/pen/ZEbgQVG"> codepen link </a>


If you're in doubt with some function or whatever ask me in the comment section! =D




Best regards,