tags:

views:

160

answers:

4

Hello everybody I've recently asked this question and I've given up with the 'replace input idea'. Instead I've decided to try another approach..

I've made my password field background transparent and I placed <label> element "behind" the password input, so that it appears to be part of the password field. Now I wrote a standard function for toggling element like this :

function togglePassword(obj) {
        var el = document.getElementById(obj);
        if ( el.style.display != 'none' ) {
         el.style.display = 'none';
        }
        else {
         el.style.display = '';
        }
       }

I trigger this function onforus and onblur from password field so that label disappears when the password field is focused but I have problems with onblur, because after I type password and hit TAB on the which takes me to login submit button then the <label> element appears with password value again and its mixed with typed password. image Here is the image of that, so I tried to come up with something new.

function togglePassword(obj) {
          var el = document.getElementById(obj);
                        var input_el = document.getElementById('password_field');
                if(input_el.value.length > 0)
                            {
                                el.style.display = 'none';
                            }
                           else {
                              if ( el.style.display != 'none' ) {
         el.style.display = 'none';
            }
            else {
             el.style.display = '';
            }
           }
                               }

Unfortunately, this code doesn't work .. can someone point to me where did I made mistake? So this second code should check whether password field is emtpy or not, if its not empty then continue to toogle if it is don't toggle just hide the password value. Help :D thank you

A: 

HI,

try to use a library like JQuery. they are really helpful in such simple tasks, and also can handle quite complex ones...

important point is: such a thing like hiding/showing is solved already hundred times. do not reinvent the wheel ;-)

cRichter
yes but what about password field, if I show password field value then the password will be masked, what about that?So I need to change the field type or do something more .. "simple"
c0mrade
+1  A: 

One day I'll get round to writing up the details of this technique, but a working implementation has been sitting in my tmp directory for the last six months: http://dorward.me.uk/tmp/label-work/example.html

David Dorward
You gave me this link before, just I couldn't find it , but I posted the previous question before hoping you'd answer, thank you first of all, second .. is this possible to do without jquery? Because I'm using prototype for this project and I just got in the middle of everything so I try to make everything which needs javascript by writing plain old javascript. thank you
c0mrade
Of course. You just need to port the logic. (Which is the add the class to hide the label gets the focus, and onload if the field has a non-empty value, and remove it onblur but only if the field has an empty value)
David Dorward
Well said .. that is exactly what I was trying to do, see my "script" attempt above, where did I go wrong. Maybe #1 error I tried to hide label on field focus .. instead of label focus function
c0mrade
+2  A: 
valli
This does work indeed :-)
c0mrade
One more thing .. I put the togglePassword function onblur and onfocus within password_field iput. Now when I click the empty field the password dissapears and works great, but when I click on the password itself(<label> element) .. nothing happens .. is it possible to fix that ?
c0mrade
Try to put lable and password field both in a Div and give id for that. By giving like this you can toggle only field or total lable and field
valli
I just added same function to the label on click .. it works thank you .. just added .focus() on the password field
c0mrade
A: 

I have not quite understand the requirement, but you should verify in your function if you are doing a blur on focu action and reacting on this.

Perhaps something like that :

<script type="text/javascript">
function togglePassword(alwaysVisible, labelId, pwdId){
 var label=document.getElementById(labelId);
 if (alwaysVisible){
     label.style.display="";
 } else {
     var pwd=document.getElementById(pwdId);
     label.style.display= (pwd.value.length==0)?"":"none";
 }
} 
</script>

....

<form>
 <label  for="pwd" id="labelPwd">Type your password&nbsp;:&nbsp;</label>
 <input name="pwd" id="pwd" onfocus="togglePassword(true, 'labelPwd', 'pwd')" onblur="togglePassword(false    , 'labelPwd', 'pwd')" type="password">

 <input type="submit">                                                               
</form>
Patrick