6

I have the password field on page. I want to display text "Enter password" on screen before entering password but on focus when user enter password it should go back to password type

EDIT: I am using Jquery as well so any small jquery solution will do

jasonbar
  • 12,475
  • 3
  • 35
  • 46

6 Answers6

11

[Latest revisision to include IE support]
Update for IE9: Version 9 of IE allows the change of the type attribute for input elements of type text/password back and forth

As mentioned in the comments (and verified) the previous examples did not work in IE as it does not allow the change of the type by script ... Here is a workaround, which replaces the element with another back-and-forth (code assumes you start with a text box)

var element = document.getElementById('mysearch');

var text_to_show = 'Enter Password';

element.value = text_to_show; // set the message for the first time

element.onfocus = function(){ 
                       if (this.value == text_to_show) 
                           {
                             var newElement = convertType(this);
                            newElement.value = '';
                            setTimeout(function(){document.getElementById(newElement.id).focus()},100);
                           }
                       }
element.onblur = function(){ 
                       if (this.value == '') 
                          {
                            var newElement = convertType(this);
                            newElement.value = text_to_show;
                          }
                    }


function convertType(elem)
{
    var input = document.createElement('input');
    input.id = elem.id;
    input.value = elem.value;
    input.onfocus = elem.onfocus;
    input.onblur = elem.onblur;
    input.className = elem.className;
    if (elem.type == 'text' )
      { input.type = 'password'; }
    else
      { input.type = 'text'; }

    elem.parentNode.replaceChild(input, elem);         
  return input;
}

[update]

scrap the original answer, i missed the part that you want to keep the field as password (with hidden contents)

Revised answer:

var element = document.getElementById('mysearch');

var text_to_show = 'Enter Password';
element.type="text"; // set the type to text for the first time
element.value = text_to_show; // set the message for the first time
element.onfocus = function(){ 
                       if (this.value == text_to_show) 
                           {
                            this.type="password";
                            this.value = '';
                           }
                       }
element.onblur = function(){ 
                       if (this.value == '') 
                          {
                            this.type="text";
                            this.value = text_to_show;
                          }
                       }

[original answer]

var element = document.getElementById('inputID'); 
//  inputID should be the ID given to the password element

var text_to_show = 'Enter Password'
element.value = text_to_show;
element.onfocus = function(){ if (this.value == text_to_show) this.value = '';}
element.onblur = function(){ if (this.value == '') this.value = text_to_show;}

Gabriele Petrioli
  • 173,972
  • 30
  • 239
  • 291
  • That wouldn't work on a password field, as `Enter Password` will appear as `***********`. – Esteban Küber Mar 12 '10 at 02:40
  • @voyager, yes i missed that part in the question .. revised answer to correct this – Gabriele Petrioli Mar 12 '10 at 02:41
  • @Gaby, just one thing: have you tested if this works on IE6-8, Opera and Chrome? My gut feeling is that IE6 *might* be a problem. – Esteban Küber Mar 12 '10 at 13:43
  • Good point @voyager, i have not tried it at IE6.. will do so later at home and post the results and if needed workarounds.. – Gabriele Petrioli Mar 12 '10 at 15:27
  • 1
    IE (all versions, as far as I know) doesn't allow you to change the type between text and password. – Matthew Crumley Mar 12 '10 at 16:09
  • @Matthew & @voyager, Added cross-browser version (it replaces the element..).. tested with IE6, FF, Safari, Opera, Chrome.. – Gabriele Petrioli Mar 13 '10 at 16:02
  • But guys- what about the Browsers password manager & firefox caching? After page load the browser inject content, if from the manager or the cache, than what? How to get those events? .select() / .change() / or any other event i tried to attach to the element, just didn't manage to catch. – neoswf May 10 '11 at 18:09
3

You can use this jQuery plugin, which supports password fields.

SLaks
  • 800,742
  • 167
  • 1,811
  • 1,896
3

You can either give it an image background with the text Enter Password that you change dynamically using javascript (ideally by just removing a CSS class),

<input type="password" class="enter-password"> or
<input type="password" style="background-image:url('enter-password.png');">

or place a fake input that you replace with javascript for a password input.

I'm not sure how well it would cross browser to change the type of input on the fly.

document.getElementsByTagName("input")[0].type = "text" /* change a hidden field to text*/ works on Firefox, but I wouldn't rely on it working well on IE without testing.

Esteban Küber
  • 33,970
  • 13
  • 78
  • 96
  • But your are using bytes for an image for nothing when you can do it pure Javascript. Also you do not want a password field to become text if you entered something into it revealing your password. – Jonathan Czitkovics Mar 12 '10 at 02:58
  • @Jon: See Gaby's answer http://stackoverflow.com/questions/2430109/how-display-text-in-password-field/2430161#2430161 That's what I'm proposing. The only reason I also proposed the image based solution is out of concern of good behavior of the various browsers out there. – Esteban Küber Mar 12 '10 at 13:54
  • This is the easiest and least-intrusive solution. Bravo. – DWRoelands Nov 21 '14 at 19:45
3

If you don't want to use a plugin (like the one in SLaks' answer), you have to either position a label above the password field (what the plugin does), or hide the password field and show a text input in its place until it gets the focus.

Internet Explorer doesn't let you change an input's type from "password" to "text", so any solution that tries to do that won't work in IE.

Here's an example that works in at least IE7 (it should work in IE6, but I haven't tried it), Chrome, and Firefox.

jQuery(function($) {
    function make_label_field(password_input, label) {
        var new_input = document.createElement("input");
        new_input.type = "text";
        new_input.size = password_input.size;
        new_input.className = password_input.className;
        new_input.setAttribute("style", password_input.getAttribute("style"));
        // Copy any additional properties you need. You may want to add a class
        // to style the label differently

        new_input.value = label;

        $(new_input).focus(function() {
            $(this).hide();
            $(password_input).show().focus();
        });
        return new_input;
    }

    $("input[type=password]").each(function() {
        $(this).after(make_label_field(this, "Enter password")).hide();
    }).blur(function() {
        if (this.value == "") {
            $(this).hide().next().show();
        }
    });
});
Community
  • 1
  • 1
Matthew Crumley
  • 95,375
  • 24
  • 103
  • 125
0

[ revised answer ] Added compatibility for ie6 from this website

<form id="form1" name="form1" method="post" action="">
  <input onfocus="clear_field(this,'Enter Username')" onBlur="revert_field(this,'Enter Username')" name="username" type="text" id="username" value="Enter Username" />
  <p>
<input type="text" id="passwordtext" value="Password" onclick="switchto(1)" onkeydown="switchto(1)">
<input type="password" id="password" value="" onblur="if (this.value=='')switchto(0)" style="display:none">
  <p>
    <input  type="submit" name="button" id="button" value="Submit" />
  </p>
</form>

<script>
function switchto(q){
    if (q){
        document.getElementById('passwordtext').style.display="none";
        document.getElementById('password').style.display="inline";
        document.getElementById('password').focus();
    } else {
        document.getElementById('password').style.display="none";
        document.getElementById('passwordtext').style.display="inline";
    }
}
function clear_field(field,text){
if(field.value==text){
    field.value = "";
    }
}
function revert_field(field,text){
if(field.value==""){
    field.value = text;
    }
}
</script>

[previous post ] maybe this help.

<script>
function clear_field(field){
field.value='';
}
function change(){
document.getElementById('pass').value='';
document.getElementById('pass').type='password';

}
</script>

apis17
  • 2,645
  • 2
  • 21
  • 23
0

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";
Jonathan Czitkovics
  • 1,612
  • 11
  • 10