3

I am trying to make where if the user clicks on the checkbox, it shows the password in the inputbox as text. And if he/she unchecks, it sets the attribute to password. Any pointers?

Code:

 <!DOCTYPE html>
<html>
<head>
    <script type='text/javascript' src='jquery-3.1.1.min.js'></script>
    <script type='text/javascript' src='jquery-ui/jquery-ui.js'></script>

    <script type='text/javascript'>
        $(document).ready(function(){
            $('#check').click(function(){
                if($('test-input').attr('type', 'text'));
            });
        });
    </script>
</head>

<body>
    <input type='password' id='test-input' /> Show password <input type='checkbox' id='check' />
</body>
</html>
BoeNoe
  • 510
  • 1
  • 6
  • 18
  • 2
    Possible duplicate of [change type of input field with jQuery](http://stackoverflow.com/questions/1544317/change-type-of-input-field-with-jquery) – Alex Sep 27 '16 at 13:57

6 Answers6

8

Do this :

$('#check').click(function(){
    if('password' == $('#test-input').attr('type')){
         $('#test-input').prop('type', 'text');
    }else{
         $('#test-input').prop('type', 'password');
    }
});
jean-max
  • 1,370
  • 11
  • 29
8

You are missing a # before test-input in the selector and you need to check the state of the checkbox each time you click on it

<script type='text/javascript'>
        $(document).ready(function(){
            $('#check').click(function(){
             alert($(this).is(':checked'));
                $(this).is(':checked') ? $('#test-input').attr('type', 'text') : $('#test-input').attr('type', 'password');
            });
        });
    </script>
jacobdo
  • 1,245
  • 3
  • 10
  • 28
3

This work correct:

$('#check').click(function(){
 if(document.getElementById('check').checked) {
    $('#test-input').get(0).type = 'text';
  } else {
      $('#test-input').get(0).type = 'password';
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type='password' id='test-input' /> Show password <input type='checkbox' id='check' />

JSFIDDLE: https://jsfiddle.net/0xuue3v6/

patwoj98
  • 429
  • 2
  • 14
3

I've made this little snippet:

$('.toggle-password').click(function(){
    $(this).children().toggleClass('mdi-eye-outline mdi-eye-off-outline');
    let input = $(this).prev();
    input.attr('type', input.attr('type') === 'password' ? 'text' : 'password');
});
<link href="https://cdnjs.cloudflare.com/ajax/libs/MaterialDesign-Webfont/4.7.95/css/materialdesignicons.css" rel="stylesheet"/>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js" integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6" crossorigin="anonymous"></script>


<div class="form-group col-6">
   <label>Password</label>
   <div class="input-group">
      <input type="password" class="form-control" name="password" autocomplete="new-password" minlength="6">
      <div class="input-group-append toggle-password">
           <span class="input-group-text mdi mdi-eye-outline"></span>
      </div>
   </div>
</div>

It works with jQuery, for styling I've used Bootstrap and the icons are from Material Icons.

Jonathan Arias
  • 254
  • 2
  • 14
1

Your condition has a missing # for an id. Also that will set the type instead of checking if the type is text or password.

$(document).ready(function() {
  $('#check').click(function() {
    if ($('#test-input').attr('type') == 'text') {
      $('#test-input').attr('type', 'password');
    } else {
      $('#test-input').attr('type', 'text');
    }
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>


<input type='password' id='test-input' />Show password
<input type='checkbox' id='check' />
depperm
  • 8,490
  • 4
  • 37
  • 57
0
$('.pwd-hs-btn').click(function () {
    var next = $(this).next();
    if ('password' == next.attr('type')) {
      next.prop('type', 'text');
    } else {
     next.prop('type', 'password');
    }
});
Tayyeb
  • 39
  • 6