admin管理员组文章数量:1265450
i have username and password textboxes. i want them to both show the default text, ie "username" and "password", but when the user clicks on the password box it should dynamically change to a "password" type for security reasons.
i've managed to do this easily enough with javascript code but it doesn't want to work in IE.
<input id="username" class="username" name="u" type="text" value="username" onclick="if(this.value='username') {this.value=''};" />
<input id="password" class="password" name="p" type="text" value="password" onclick="if(this.value='password') {this.value=''; this.type='password'};" />
i have username and password textboxes. i want them to both show the default text, ie "username" and "password", but when the user clicks on the password box it should dynamically change to a "password" type for security reasons.
i've managed to do this easily enough with javascript code but it doesn't want to work in IE.
<input id="username" class="username" name="u" type="text" value="username" onclick="if(this.value='username') {this.value=''};" />
<input id="password" class="password" name="p" type="text" value="password" onclick="if(this.value='password') {this.value=''; this.type='password'};" />
Share
Improve this question
edited May 4, 2011 at 4:48
Dkong
asked May 4, 2011 at 4:43
DkongDkong
2,78812 gold badges54 silver badges74 bronze badges
3
- 1 Can, you post your sample codes? – Sai Kalyan Kumar Akshinthala Commented May 4, 2011 at 4:44
-
4
Assuming you get it working (from one of the answers below), you should also change the trigger from
onclick
toonfocus
, to allow for when the user tabs between fields rather than using the mouse. – nnnnnn Commented May 4, 2011 at 5:02 - 1 Shouldn't the condition read "==" (instead of "=")? – Tom Commented Jul 26, 2012 at 23:07
5 Answers
Reset to default 6Let say your html code is like this
<input type="text" id="txtId" />
you can replace it by using Jquery,
$('#txtId').replaceWith('<input type="password" id="txtId" />')
IE only lets you set the type of an input
element once, when the element is created. Attempts to set it after that will fail.
If you want to "change" the element in IE, what you'll likely need to do is create another element with the same attributes (except for the type, of course), and replace the existing element with it.
Or create a dummy textbox for the password, and have the real password box hidden -- on focus the dummy box should hide itself, and show and focus on the real password box.
Would you consider using JQuery?
$("[name=fieldname]").attr("type", "password");
Here is a function to replace any form element by a password with the same value, knowing its id:
function replaceWithPassword(id)
{
var o = $('input#' + id + ', textarea#'+id + ', select#'+id);
o.replaceWith('<input type="password" value="' + o.val() + '" id="' + id + '" />');
}
This worked for me Put this in the head section
function changeBack()
{
if(document.getElementById('smsl_pw').value=='')
{
document.getElementById('smsl_pw').type='text';
document.getElementById('smsl_pw').value='Password';
}
}
function changePass()
{
document.getElementById('smsl_pw').type='password';
document.getElementById('smsl_pw').value='';
}
Here is the input field
<input id="smsl_pw" onclick="changePass();" onblur="changeBack();" name="smsl_pw" type="text" value="Password">
Hope it helps someone
本文标签: javascriptprogramatically change textbox type from type39text39 to type39password39Stack Overflow
版权声明:本文标题:javascript - programatically change textbox type from type='text' to type='password' - Stack Ove 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741062959a2333014.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论