Show plain text in a password field
by
, 06-06-2012 at 12:51 PM (47534 Views)
I was browsing the internet and came across this tecnique and thought it was rather cool.
To make plain text show in a password field, you need the following form -
Run this function once the page has loaded (HTML Code:<form> <input type="text" name="passwordPlain" id="passwordPlain" value="Password" onfocus="swapPasswordBoxes('click')" style="display:none;"/> <input type="password" name="password" id="password" value="" onblur="swapPasswordBoxes('blur')" /> </form><body onload="onPageLoaded()">
)
And add this function to your javascript as wellHTML Code:function onPageLoaded() { document.getElementById('password').style.display = "none"; document.getElementById('passwordPlain').style.display = "inline"; }
This means that the plain text box is visible to the user when they load the page (if they have javascript {else it just shows the standard password box}).HTML Code:function swapPasswordBoxes(funcType) { if(funcType == "click") { document.getElementById('passwordPlain').style.display = "none"; document.getElementById('password').style.display = "inline"; document.getElementById('password').focus(); } else { if(document.getElementById('password').value.length == 0) { document.getElementById('passwordPlain').style.display = "inline"; document.getElementById('password').style.display = "none"; } } }
Then when they click on the plain text box, that box is made invisible and the password box is made visible (and it sets the focus to this box as well). If they click off the password box, and the box is empty, it is reset to the plain text box.
Full working code -
HTML Code:<!DOCTYPE html> <html> <head> <script type="text/javascript"> function onPageLoaded() { document.getElementById('password').style.display = "none"; document.getElementById('passwordPlain').style.display = "inline"; } function swapPasswordBoxes(funcType) { if(funcType == "click") { document.getElementById('passwordPlain').style.display = "none"; document.getElementById('password').style.display = "inline"; document.getElementById('password').focus(); } else { if(document.getElementById('password').value.length == 0) { document.getElementById('passwordPlain').style.display = "inline"; document.getElementById('password').style.display = "none"; } } } </script> </head> <body onload="onPageLoaded()"> <form> <input type="text" name="passwordPlain" id="passwordPlain" value="Password" onfocus="swapPasswordBoxes('click')" style="display:none;"/> <input type="password" name="password" id="password" value="" onblur="swapPasswordBoxes('blur')" /> </form> </body> </html>