1

I would like to create a registration form with hide-able and revealable password input. I tried this (JS):

function showhide(melyik,hol) {
    var melyiket = document.getElementById(melyik);
    var melyikkel = document.getElementById(hol);

    if (melyikkel.value == '1') {
        document.getElementById(melyikkel).value='0';   
        document.getElementById(melyiket).type='password'; 
    } else {
        document.getElementById(melyikkel).value='1';   
        document.getElementById(melyiket).type='text';                 
    } 
}

Form:

<div style="margin-top:20px;">
    <p>SQL host: <input type="text" name="sql_host" value="localhost"/></p>
    <p>SQL adatbázis: <input type="text" name="sql_db"/></p>
    <p>SQL felhasználó: <input type="text" name="sql_user"/></p>
    <p>SQL jelszó: <input type="text" name="sql_password" id="sql_password"/>
        <input type="checkbox" name="show_password_1" id="show_password_1" value="0" onclick="showhide('sql_password','show_password_1');">
    </p>
</div>

I want to do this: If checkbox is checked the password input type is text. When not checked the password type is password... I wrote this JavaScript, but not working. :-(

1

1 Answer 1

2

Use .checked to see if the checkbox is selected, not value. Here's a working example.

HTML:

<input type="password" name="pwd" id="pwd" />
<input type="checkbox" name="show" id="show" onclick="showhide('pwd','show');">​

JS:

function showhide(pwd,show)
{
    var epwd = document.getElementById(pwd);
    var eshow = document.getElementById(show);

    epwd.type = eshow.checked ? 'text' : 'password';                 

}​
Sign up to request clarification or add additional context in comments.

3 Comments

Note that changing an input type dynamically is not supported in IE.
@jbabey Works fine in IE9.
IE is piggeon c*ap... :-D I don't use them... But this is it, what i wanted exactly...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.