There are several ways to do it but here is one way.
I'm not saying that this is efficient but this will explain better of what is happening.
Copy and paste this to try it out!
<body>
<script type="text/javascript">
text=document.createElement("input");
text.type="text";
text.value="password";
text.setAttribute("onclick", 'toPassword();');
text.setAttribute("onblur", 'toText();');
document.getElementsByTagName("body")[0].appendChild(text);
function toText()
{
if(document.getElementsByTagName("input")[0].value=="password" || document.getElementsByTagName("input")[0].value=="")
{
document.getElementsByTagName("input")[0].type="text";
document.getElementsByTagName("input")[0].value="password"
}
}
function toPassword()
{
if(document.getElementsByTagName("input")[0].value=="password" || document.getElementsByTagName("input")[0].value=="")
{
document.getElementsByTagName("input")[0].type="password";
document.getElementsByTagName("input")[0].value=""
}
}
</script>
</body>
It creates a text field with value of password
and then when you click in it, it then changes to password field and removes its value. If you click and you did not enter anything then it will return to a text
and change its value to password
.
You may also enter a text field along with less javascript code too, all you would need is the functions.
If you want this to work in IE you would need to create two inputs, one for text and one for password and alternate display=none
and display=block
on each of them.
element.style.display="none";