I am trying to change type of input, specifically after click into input change type="text" to change="password".
I don't know why, but I can't to add the option type="password" as attribute to input.
$('input#id_reg_pass').removeAttr("type"); #this works, type="text" is removing
$('input#id_reg_pass').attr('type', 'password'); #problem, this not works
$(this).addClass('exampleText').val($(this).attr('title'));
Haven't someone similar problem? I'll glad for every hint... Thank you
Your code throws an error for me,
type property can't be changed
. Interestingly though, the following code does work for me on Chrome at least:It could be that this doesn't work cross-browser however, which would explain why jQuery doesn't allow it (As pointed out by Niklas, this is the case). Alternatively you could construct a new
<input>
with the same attributes and replace the old one with it.You cannot change the attribute
type
after adding the element to the DOM, if you want it to function the same way on all browsers, see this and this.Try this demo is here
If you're using jQuery 1.6, use
.prop
instead:Demo: http://jsfiddle.net/z8Rj3/
I had exactly the same problem, I wanted to show a text in a password field until the user clicked over it. I did another approach, instead to try to change the type of the field I hide it and showed a new text field, then called the focus(). It works fine.
where #password_fake is a text field and #password a password field with style="display:none".