Input type="password" with password display eye

Asked

Viewed 34,793 times

37

I need to put a <input type="password"> with that eye of the reveal password, but it has to work as follows:

User clicks on the eye and when dropping hides the characters again.

Follow an example photo:

inserir a descrição da imagem aqui

  • 1

    What have you tested?

  • 2

    What many web pages use is a checkbox mostrar a senha. Check out this interesting article on this http://tableless.com.br/precis-confirmpassword/

  • 4

    You already have excellent answers, so I’ll leave only this way using bootstrap to try to show you "another" shape (it’s the same one that has already been presented, but in a "different way").

  • In all solutions below there is a problem that in Edge appears what the browser automatically puts, and done manually.

  • @David did not understand, can explain more clearly ?

  • 2

    can yes @devgaspa, I opened this question in the browser: Microsoft Edge, and the browser itself puts the "eye" in password fields, thus being duplicated the icon.

Show 1 more comment

9 answers

34


Using jquery, do the following:

$( "#olho" ).mousedown(function() {
  $("#senha").attr("type", "text");
});

$( "#olho" ).mouseup(function() {
  $("#senha").attr("type", "password");
});

Complete Example:

var senha = $('#senha');
var olho= $("#olho");

olho.mousedown(function() {
  senha.attr("type", "text");
});

olho.mouseup(function() {
  senha.attr("type", "password");
});
// para evitar o problema de arrastar a imagem e a senha continuar exposta, 
//citada pelo nosso amigo nos comentários
$( "#olho" ).mouseout(function() { 
  $("#senha").attr("type", "password");
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script>
<input type="password" value="123456789" id="senha">
<img id="olho" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAABDUlEQVQ4jd2SvW3DMBBGbwQVKlyo4BGC4FKFS4+TATKCNxAggkeoSpHSRQbwAB7AA7hQoUKFLH6E2qQQHfgHdpo0yQHX8T3exyPR/ytlQ8kOhgV7FvSx9+xglA3lM3DBgh0LPn/onbJhcQ0bv2SHlgVgQa/suFHVkCg7bm5gzB2OyvjlDFdDcoa19etZMN8Qp7oUDPEM2KFV1ZAQO2zPMBERO7Ra4JQNpRa4K4FDS0R0IdneCbQLb4/zh/c7QdH4NL40tPXrovFpjHQr6PJ6yr5hQV80PiUiIm1OKxZ0LICS8TWvpyyOf2DBQQtcXk8Zi3+JcKfNafVsjZ0WfGgJlZZQxZjdwzX+ykf6u/UF0Fwo5Apfcq8AAAAASUVORK5CYII="
/>

  • 1

    Sorry buddy, but in the eye I put what?

  • 2

    Put that - id="olho"

  • 1

    In your html, find where that "eye" is and add the "id" attribute to it by putting it like @Taopaipai said. :)

  • +1 Top solution.

  • Perfect answer, very useful! ;) +1

  • @Vaati, it was great, I did a little different, I’ll put as I did, it was top! Instead of using IMG I used an Icon... Thank you very much!

  • 4

    If you drag your eye and drop the click away, the password is exposed.

  • 1

    Added resolution code.

Show 3 more comments

22

Using Javascript only, you can add to an element, in this case it is the tag img, two events (mousedown and mouseup), these will change the property type of input, alternating between text and password.

Take an example:

document.getElementById('olho').addEventListener('mousedown', function() {
  document.getElementById('pass').type = 'text';
});

document.getElementById('olho').addEventListener('mouseup', function() {
  document.getElementById('pass').type = 'password';
});

// Para que o password não fique exposto apos mover a imagem.
document.getElementById('olho').addEventListener('mousemove', function() {
  document.getElementById('pass').type = 'password';
});
#pass {
  width: 150px;
  padding-right: 20px;
}

.olho {
  cursor: pointer;
  left: 160px;
  position: absolute;
  width: 20px;
}
<img src="https://cdn0.iconfinder.com/data/icons/ui-icons-pack/100/ui-icon-pack-14-512.png" id="olho" class="olho">
<input type="password" id="pass">

  • very good, thank you.

  • Excellent, helped me a lot too! Thanks @devgaspa

  • 2

    If you drag your eye and drop the click away, the password is exposed.

  • @bfavaretto changed the code, I found no option right away but the implemented, if you know any could share ? Thanks.

  • It may also be in the dragend.

  • @bfavaretto by what I noticed, what happens is the following: when you move the mouse with the button pressed (mousedown) to release he does not recognize that the mouseup is of that element ?

  • 1

    @devgaspa that’s right, the mouseup takes place outside. Which means you can also solve by delegation :)

  • @bfavaretto I will open a question, I would like to see the example with delegation.

  • 1

    Sergio has set an example with delegation in his response now.

  • @bfavaretto can not find the answer, can inform me the link ?

  • Right down here :D

Show 6 more comments

14

Some of the other answers show how to show/hide the password when you click the input, regardless of where you click.

To click to a specific place, eye or text you need to use another element, an image for example:

var input = document.querySelector('#input input');
var img = document.querySelector('#input img');
img.addEventListener('click', function () {
  input.type = input.type == 'text' ? 'password' : 'text';
});
#input > * {
  height: 1.3em;
  float: left;
}

#input img {
  cursor: pointer;
}
<div id="input">
  <input type="password" value="password" />
  <img src="http://i.stack.imgur.com/H9Sb2.png" alt="">
</div>

jsFiddle: http://jsfiddle.net/cj3kmxL4/


Edit:

To only show the text on mousedown and hiding again can be used like this: http://jsfiddle.net/cj3kmxL4/1/, that in the background uses the events mouse(up|down) and listen to the event mouseup in window, in case the mouseup be outside the element.

var input = document.querySelector('#input input');
var img = document.querySelector('#input img');
var visivel = false;
img.addEventListener('mousedown', function () {
  visivel = true;
  input.type = 'text';
});
window.addEventListener('mouseup', function (e) {
  if (visivel) visivel = !visivel;
  input.type = 'password';
});
  • 1

    Who gave the -1 can comment if there is something wrong in my reply...

  • 1

    I found it interesting the way you used the ternary operator.

  • Sergio, great method used in your code to treat the mouseup outside the element.

7

I made an example using the bootstrap to create the eye and the script used is basically the same used in the other answers.

var $spnMostrarSenha = $('#spnMostrarSenha');
var $txtSenha = $('#txtSenha');

$spnMostrarSenha
  .on('mousedown mouseup', function() {
    var inputType = $txtSenha.attr('type') == 'password' ? 'text' : 'password';
    $txtSenha.attr('type', inputType);
  })
  .on('mouseout', function() {
    $txtSenha.attr('type', 'password');
  });
.form-group .glyphicon-eye-open {
  pointer-events: auto;
}
.form-group .glyphicon-eye-open:hover {
  cursor: pointer;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="form-group">
  <input type="password" class="form-control" id="txtSenha" value="password">
  <span id="spnMostrarSenha" class="glyphicon glyphicon glyphicon-eye-open form-control-feedback"></span>
</div>

  • 1

    Unlike the others this code on IE 11 no two eyes appear. + 1

2

Guys look at the solution I made, it was exactly the way I wanted it...

HTML:

<div class="form-group col-sm-6 submit-line">
                <label>Senha:<span class="stf-color-asterisco">*</span></label>
                <div class="submit-line">
                    <input type="password" class="form-control" id="uePassowrd" />
                    <button class="submit-eye" type="submit">
                        <i id="ueEyePass" class="fa fa-eye"></i>
                    </button>
                </div>
            </div>

javascript:

            $("#ueEyePass").mousedown(function () {
                $("#uePassowrd").attr("type", "text");
            });

            $("#ueEyePass").mouseup(function () {
                $("#uePassowrd").attr("type", "password");
            });

css:

.submit-eye {
              position:absolute;
              top:5px; right:0;
              z-index:10;
              border:none;
              background:transparent;
              outline:none;
            }

            .submit-line {
              position: relative;
              /*width: 600px;*/
            }

            .submit-line input {
              width: 100%;
            }

1

I managed to do it that way, in case anyone’s interested.

<center>

<script>                        
function show() {
  var senha = document.getElementById("senha");
  if (senha.type === "password") {
    senha.type = "text";
  } else {
    senha.type = "password";
  }
}
</script>

<input type="password" name="senha" id="senha" placeholder="Digite a Senha" required>
<input type="checkbox" onclick="show()">
<br><br>
<input type="submit" value="Enviar">

</center>

1

Pure Javascript

Shows the password while clicking and while the mouse is over the eye is showing no need to mouse pressed (I find it tiring) and just remove the mouse from the eye to hide the password.

function mouseoverPass(obj) {
  var obj = document.getElementById('myPassword');
  obj.type = "text";
}
function mouseoutPass(obj) {
  var obj = document.getElementById('myPassword');
  obj.type = "password";
}
    
#myPassword {
  width: 150px;
  padding-right: 20px;
}

.olho {
  cursor: pointer;
  left: 160px;
  position: absolute;
  width: 20px;
}
Mostra a senha ao clicar e enquanto o mouse estiver sobre o olho fica mostrando sem necessidade de ficar com mouse pressionado (acho cansativo) e basta retirar o mouse de cima do olho para esconder a senha.
<br>

<input type="password" name="password" id="myPassword" size="30" />
<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAABDUlEQVQ4jd2SvW3DMBBGbwQVKlyo4BGC4FKFS4+TATKCNxAggkeoSpHSRQbwAB7AA7hQoUKFLH6E2qQQHfgHdpo0yQHX8T3exyPR/ytlQ8kOhgV7FvSx9+xglA3lM3DBgh0LPn/onbJhcQ0bv2SHlgVgQa/suFHVkCg7bm5gzB2OyvjlDFdDcoa19etZMN8Qp7oUDPEM2KFV1ZAQO2zPMBERO7Ra4JQNpRa4K4FDS0R0IdneCbQLb4/zh/c7QdH4NL40tPXrovFpjHQr6PJ6yr5hQV80PiUiIm1OKxZ0LICS8TWvpyyOf2DBQQtcXk8Zi3+JcKfNafVsjZ0WfGgJlZZQxZjdwzX+ykf6u/UF0Fwo5Apfcq8AAAAASUVORK5CYII=" onclick="mouseoverPass();" onmouseout="mouseoutPass();"  id="olho" class="olho"/>

    

-3

<style type="text/css">
  #exibe{
height: 50px;
width: 50px;
background: #000;
  }
</style>

<script src="https://code.jquery.com/jquery-1.7.1.js"></script>

<script>
  $(function($){
      var $senha = $("#senha");
      $("#exibe").on('mouseup mousedown', function(resposta1){
        if ($senha.attr('type') == 'password') {
            $senha.attr("type", "text");
            }else{
            $senha.attr('type', 'password');
            }
          });
      });
</script>

<input type="password" id="senha" value="password" />
<div id="exibe"></div>

-6

var senha = $('#senha');
var olho= $("#olho");

olho.mousedown(function() {
  senha.attr("type", "text");
});

olho.mouseup(function() {
  senha.attr("type", "password");
});
// para evitar o problema de arrastar a imagem e a senha continuar exposta, 
//citada pelo nosso amigo nos comentários
$( "#olho" ).mouseout(function() { 
  $("#senha").attr("type", "password");
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script>
<input type="password" value="123456789" id="senha">
<img id="olho" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAABDUlEQVQ4jd2SvW3DMBBGbwQVKlyo4BGC4FKFS4+TATKCNxAggkeoSpHSRQbwAB7AA7hQoUKFLH6E2qQQHfgHdpo0yQHX8T3exyPR/ytlQ8kOhgV7FvSx9+xglA3lM3DBgh0LPn/onbJhcQ0bv2SHlgVgQa/suFHVkCg7bm5gzB2OyvjlDFdDcoa19etZMN8Qp7oUDPEM2KFV1ZAQO2zPMBERO7Ra4JQNpRa4K4FDS0R0IdneCbQLb4/zh/c7QdH4NL40tPXrovFpjHQr6PJ6yr5hQV80PiUiIm1OKxZ0LICS8TWvpyyOf2DBQQtcXk8Zi3+JcKfNafVsjZ0WfGgJlZZQxZjdwzX+ykf6u/UF0Fwo5Apfcq8AAAAASUVORK5CYII="
/>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.