views:

92

answers:

3

According to my title how to implement the onfocus and onblur to the password field? Default value is password (readable) and when we click the password field it will return to password format.

Here an example, I don't want to see "●●●●●●●●" but "Password" instead

<input type="password" name="password" value="password" onfocus="this.value=(this.value=='password') ? '' : this.value;" onblur="this.value=(this.value=='') ? 'password' : this.value;" />

Update

Google result I found Change Input Type Dynamically but the code is too long. Maybe jquery can make it shorter.

+1  A: 

change the type attr. dynamically:

<input type="text" name="email" value="[email protected]" onfocus="this.value=(this.value=='[email protected]') ? '' : this.value;this.type='password';" onblur="this.value=(this.value=='') ? '[email protected]' : this.value;this.type='text';" />

(I suggest you do this with a script instead of inline).

digitalFresh
You can't do this cross-browser, in IE specifically.
Nick Craver
it will returned to `type="text"`
kampit
@Nick: that is interesting. Side-effect of only testing it on Chrome. My only other suggestion is to maybe create a `text` *and* `password` element, and juggle them :P
digitalFresh
+4  A: 

You will have trouble in Internet Explorer if you try and use 1 field because IE will not let you change the type of field.

You'll need to use 2 fields, and when the text input is focused, hide it, show the password input, and focus it.

You'd need something like this (using jQuery):

<input type="text" id="fakeemail" value="[email protected]"/>
<input type="password" id="realemail" value="" name="email" style="display:none;"/>

$('#fakeemail').focus(function(){
  $(this).hide();
  $('#realemail').show().focus();
});
$('#realemail').blur(function(){
  if(this.value == ''){
    $(this).hide();
    $('#fakeemail').show();
  }
});  
scunliffe
Check the input's value in blur and it's a great answer: `$('#realemail').blur(function(){ if(this.value == "") { $(this).hide(); $('#fakeemail').show();}});`
digitalFresh
hmmm.. I don't see how it work. http://jsfiddle.net/nzyrx/ I just update my question.
kampit
A: 

You can try using some jQuery and moving field labels around with CSS positioning. Check this guy's implementation out: http://graphicpush.com/more-accessible-input-pre-filled-text-with-jquery

The site that he links to also has a nice fade effect on the input fields on focus (but his implementation is a bit nicer than the one he links to).

hollsk
can be done for `type="password"` ?
kampit
There's no reason why it shouldn't - the type of the field isn't important because it's being masked by its associated label (which will be 'password' as readable text). This is a good approach because people who don't have javascript turned on still get the field hinting, so it's accessible too, as well as being more semantically correct than using a dummy input field, for eg.
hollsk
@holisk but he wants the user to be able to read the actual password, not just the label "Password".
Pointy
But how can the user read their password before they've entered anything into the box? That's a level of magic that I don't think even jQuery can manage :/
hollsk