BoeNoe BoeNoe - 2 months ago 17
HTML Question

jQuery Show Password

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>

Answer

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>
Comments