Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing password field to text with checkbox with jQuery

How can I toggle a password field to text and password with a checkbox check uncheck?

like image 488
Web Worm Avatar asked Dec 28 '09 18:12

Web Worm


4 Answers

is this what you looking for ??

<html>
<head>
<script>
    function changeType()
    {
        document.myform.txt.type=(document.myform.option.value=(document.myform.option.value==1)?'-1':'1')=='1'?'text':'password';
    }
</script>
</head>

<body>
    <form name="myform">
       <input type="text" name="txt" />
       <input type="checkbox" name="option" value='1' onchange="changeType()" />
    </form>
</body>
</html>
like image 123
Michel Gokan Khan Avatar answered Oct 20 '22 17:10

Michel Gokan Khan


Use the onChange event when ticking the checkbox and then toggle the input's type to text/password.

Example:

<input type="checkbox"  onchange="tick(this)" />
<input type="input" type="text" id="input" />
<script>
function tick(el) {
 $('#input').attr('type',el.checked ? 'text' : 'password');
}
</script>
like image 43
watain Avatar answered Oct 20 '22 17:10

watain


updated: live example here

changing type with $('#blahinput').attr('type','othertype') is not possible in IE, considering IE's only-set-it-once rule for the type attribute of input elements.

you need to remove text input and add password input, vice versa.

$(function(){
  $("#show").click(function(){
    if( $("#show:checked").length > 0 ){
      var pswd = $("#txtpassword").val();
      $("#txtpassword").attr("id","txtpassword2");
      $("#txtpassword2").after( $("<input id='txtpassword' type='text'>") );
      $("#txtpassword2").remove();
      $("#txtpassword").val( pswd );
    }
    else{ // vice versa
      var pswd = $("#txtpassword").val();
      $("#txtpassword").attr("id","txtpassword2");
      $("#txtpassword2").after( $("<input id='txtpassword' type='password'>") );
      $("#txtpassword2").remove();
      $("#txtpassword").val( pswd );
    }
  });
})

live example here

like image 31
Anwar Chandra Avatar answered Oct 20 '22 18:10

Anwar Chandra


You can use some thing like this

$("#showHide").click(function () {
                if ($(".password").attr("type")=="password") {
                    $(".password").attr("type", "text");
                }
                else{
                    $(".password").attr("type", "password");
                }

    });

visit here for more http://voidtricks.com/password-show-hide-checkbox-click/

like image 34
Anand Roshan Avatar answered Oct 20 '22 17:10

Anand Roshan