11

Why isn't the jQuery changing the type attribute of #password_input to password?

<html>
    <head>
        <title></title>
        <style type="text/css">
        </style>
        <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js"></script>
        <script type="text/javascript">
        $(document).ready(function() {
            $("#password_input").attr('type','password');
        });
        </script>
    </head>
    <body>
        <div id="loginBox">
            <form>
            <input type="text" id="username_input" name="username" /><br />
            <input type="text" id="password_input" name="password" />
            </form>
        </div>
    </body>
</html>
AndrewFerrara
  • 2,273
  • 8
  • 27
  • 44

15 Answers15

25

Yes u can.

Try this its works

<input type="text" class="pwd">
  $('.pwd').click(function(){
  $(this).get(0).type='password';
});
j0k
  • 21,914
  • 28
  • 75
  • 84
Dilipkumar
  • 259
  • 1
  • 3
  • 2
11

'type' property/attribute cannot be changed.

edit: This was accurate for JQuery at the time it was posted. See other answers for how it can be done now.

JClaspill
  • 1,637
  • 18
  • 28
  • 1
    Look at bug reports on http://api.jquery.com/attr/ to see that you are not alone. Firebug will even tell you that 'property cannot be changed'. – JClaspill Apr 10 '11 at 01:37
  • 1
    I'm not sure why this keeps getting voted down... Anyone care to explain why I keep getting downvotes? – JClaspill Aug 27 '12 at 16:08
  • because people are getting confused between jquery and javascript. maybe you can edit your answer to reflect that? – owagh Nov 29 '12 at 20:19
  • @candidJ Read the post. In 2011, JQuery did not have this ability. – JClaspill Nov 22 '16 at 14:59
8

This is a security measure that most major browsers employ to mitigate screen-reading and password theft (it's more important going the other way, from password to text).

I can't tell from your code what you're trying to do, but here's my guess: you want a textbox with the word Password in it as a hint, and when it gets focus, the user enters their password as a password field. To accomplish this, you can use two elements: an <input type="text"> and an <input type="password">, the latter hidden initially. When the user focuses on the textbox, simply hide it, show the password field, and set the focus. It could get tricky toggling back and forth between them.

Update

This is now way easier to accomplish in modern browsers. Simply use something like this (here's a jsFiddle):

<input type="password" placeholder="Password" />
Jimmy Sawczuk
  • 12,908
  • 7
  • 45
  • 60
5

You can always use the pure js like:

document.getElementsByName("login_pass")[0].type="text";
Tom
  • 59
  • 1
  • 2
4

You can't change the type attribute. you must create a new element and delete the old one like this:

var originalBtn = $("#password_input");
var newBtn = originalBtn.clone();

newBtn.attr("type", "password");
newBtn.insertBefore(originalBtn);
originalBtn.remove();
newBtn.attr("id", "password_input");
Jubair
  • 2,557
  • 2
  • 26
  • 25
2

Have you tried using .prop?

$("#password_input").prop('type','password');

http://api.jquery.com/prop/

Mark Mckelvie
  • 313
  • 4
  • 13
2

This will do it

$('<input type="password" id="password_input" name="password" />').insertAfter('#password_input').prev().remove();

Check working example at http://jsfiddle.net/RyVU8/

UPDATE

If you want to show plain text in a password field and then make it a regular password field on focus you can do the following

$('#clear').focus(function() {
    $('#clear').hide();
    $('#password_input').show().focus();
});
$('#password_input').blur(function() {
    if ($('#password_input').val() == '') {
        $('#clear, #password_input').toggle();
    }
});

where #clear represents a text field that shows instead of the password field when it's in blur and user hasn't entered any password yet.

check working example at http://jsfiddle.net/uNGKb/3/

Hussein
  • 40,778
  • 25
  • 110
  • 143
1

Simply create 2 sync Input fields. One as password and one as text. Then switch their visiblity via button. Don't forget to hide the text-input on start.

<input type="password" id="inp_passwd"/><input type="password" id="inp_passwd2"/>
<input id="btn_show_passwd" type="button" value="Show Password" />

Javascript:

// sync input fields
$('#inp_passwd').keyup(function(){
    $('#inp_passwd2').val($(this).val());
});
// show clear password
$('#btn_show_passwd').mousedown(function(){
    $('#inp_passwd').hide();
    $('#inp_passwd2').show();
});
// hide clear password
$('#btn_show_passwd').mouseup(function(){
    $('#inp_passwd').show();
    $('#inp_passwd2').hide();
});
// hide hidden text-field on start
$('#btn_show_passwd').mouseup();

you can use setTimeout as well instead of mouseup

Steffomio
  • 338
  • 3
  • 6
  • That's a really elegant solution. I like the use of mouse up/down instead of a toggle. – Fi Horan Aug 23 '16 at 13:13
  • Won't work if u plan on submitting the form. The field name would have to be the same and form will pick only value of the first or last input. – Fresz Aug 29 '20 at 10:52
1
<span>Password:</span>
<input type="text" id="pwd" style="width: 180px; color: Gray;" onfocus="Changetxt();this.type='password'" onblur="if(this.value==''){this.type='text';textboxtype();}" value="Enter Your Password Here" />

 <script>

        function Changetxt() {
            if ($('#pwd').val() == "Enter Your Password Here") {
                $('#pwd').val('');
                $('#pwd').css('color', 'black');
            }
        }

        function textboxtype() {
            if ($('#pwd').val().length < 1) {
                $('#pwd').val('Enter Your Password Here');
                $('#pwd').css('color', 'gray');
            }
        }

    </script>
cdeszaq
  • 29,170
  • 25
  • 111
  • 169
0

May need to display text information in the password field made ​​the picture, set to the background of the password field, and removed when the password field focus change background image, so as to achieve the effect of prompting the user

0

I faced a similar challenge recently and i 'd like to assess my opinion. I used some of the above solutions and some other using jQuery (which i like a lot). But when test time came we realize that the solutions through jQuery and attr() function not working. So I find the appropriate solution through pure Javascript.

  var input = document.getElementById('txbOldPass');
  var newInput = document.createElement('input');

        newInput.type = 'text';
        newInput.id = input.id;
        newInput.value = input.value;
        input.parentNode.replaceChild(newInput, input);
        newInput.className = 'text';

and the opposite

  var input = document.getElementById('txbOldPass');
        var newInput = document.createElement('input');
        newInput.type = 'password';
        newInput.id = input.id;
        newInput.value = input.value;
        input.parentNode.replaceChild(newInput, input);
        newInput.className = 'password';
marc_s
  • 675,133
  • 158
  • 1,253
  • 1,388
kostas ch.
  • 1,870
  • 1
  • 16
  • 28
0

All solutions here didnt work for me (Chrome Version 51.0.2704.103 m (64-bit)). Did it like this and it works:

jQuery(function() {
    setTimeout(function(){
        jQuery('.pwForm').attr('type','password');
    }, 1);
});

Works on IE 11, too!

Wait for DOM ready and wait 1 millisecond. Then Change type=text to type=password

Rene
  • 11
  • 3
0

Use this code :

<input type="text" id="password_input" name="password" />
<script>
$("#password_input").click(function(){
    $(this).prop('type', 'date');
});
</script>
Uzair Saeed
  • 135
  • 5
0

To change the type attribute value, only need to update your JQuery.

See the example below.

<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<body>

  <div id="loginBox">
    <form>
      <input type="text" id="username_input" name="username" /><br />
      <input type="text" id="password_input" name="password" />
    </form>
  </div>
  <script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
  <script type="text/javascript">
    $(document).ready(function() {
      $("#password_input").attr('type','password');
    });
  </script>
</body>
</html>
-1

The above answers are partially wrong when they state type attribute cannot be changed. Yes, it can be changed via prop/attr jquery method. The thing is, it is supported by all browsers but IE

Otvazhnii
  • 403
  • 3
  • 10