There are several ways to do this, but this is one way. I am not saying that it is effective, but it will better explain what is happening.
Copy and paste this to try it!
<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 a password value, and then when you click on it, it then goes into the password field and removes its value. If you click and you donโt enter anything, it will return to text and change its value to password .
You can also enter a text box along with smaller JavaScript code, all you need is functions.
If you want this to work in IE, you would need to create two inputs: one for text and one for password and an alternative display=none and display=block for each of them.
element.style.display="none";
Jonathan czitkovics
source share