-5

I have created a password field with id pass, and a checkbox with id check:

<input type="password" id="pass"> 
<input type="checkbox" id="check" />Show Password

This jQuery code is used to change the password field into text field when the check box is checked, but this is not functioning correctly. The password remains as a password field even after the checkbox is checked.

$(document).ready(function () {
    $('#check').change(function () {
        $('#pass').attr('type','text');
    });
});
Sampson
  • 251,934
  • 70
  • 517
  • 549

1 Answers1

0

Add this jQuery library in the starting of your html file.

<head>
  <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
</head>

So this should be the code in your html file -

<head>
  <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
</head>

<input type="password" id="pass"> 
<input type="checkbox" id="check" />Show Password
<script>
  $(document).ready(function () {
    $('#check').change(function () {
      if($('#pass').attr("type")=="text") {
        $('#pass').attr('type','password');
      } else {
        $('#pass').attr('type','text');
      }
    });
  });
</script>

I have tested this code and it is working perfectely.

prashant
  • 108
  • 7