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. :-(

Cerbrus
  • 70,800
  • 18
  • 132
  • 147
János Tigyi
  • 397
  • 1
  • 9
  • 20
  • possible duplicate of [Changing the type in IE with JavaScript](http://stackoverflow.com/questions/2566394/changing-the-input-type-in-ie-with-javascript) – jbabey Jan 03 '13 at 13:34

1 Answers1

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';                 

}​
Paul Fleming
  • 24,238
  • 8
  • 76
  • 113