1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Change text box to password type

Discussion in 'JavaScript' started by xxKillswitch, Aug 11, 2009.

  1. #1
    Hello all, I am working on a script that allows an administrative login to manage a website via the backend. What I am doing is leaving the password box as a plain text box so that the admin can see their password as it's typed ( no masking ).

    What I want to do is offer a checkbox that when checked, will change the password text box type to password rather than text. Can anyone help me accomplish this? I don't know javascript, but I can read and understand if you can point me in the right direction.
     
    xxKillswitch, Aug 11, 2009 IP
  2. lp1051

    lp1051 Well-Known Member

    Messages:
    163
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    108
    #2
    Hi,

    the whole thing is in 'onclick' event attached to checkbox. If checked, then it changes the type of field called "pass" into password, else back to text...

    
    <form>
        Password: <input type="text" name="pass" />
        Hide password: <input type="checkbox" onclick="pass.type=(this.checked)?'password' :'text'" />
    </form>
    
    HTML:
     
    lp1051, Aug 11, 2009 IP
  3. xxKillswitch

    xxKillswitch Peon

    Messages:
    331
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks, seems to work like a charm!
     
    xxKillswitch, Aug 11, 2009 IP
  4. bholu

    bholu Peon

    Messages:
    11
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    yes this is the correct way script given
     
    bholu, Aug 15, 2009 IP
  5. dimitar christoff

    dimitar christoff Active Member

    Messages:
    882
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    90
    #5
    er, i would not recommend referencing an element property by 'pass.type'. you should write this through such a call:

    add id="pass"

    document.getElementById("pass").setAttribute("type", (this.checked) ? "text" : "password");
     
    dimitar christoff, Aug 15, 2009 IP
  6. neocoder

    neocoder Peon

    Messages:
    10
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    This solution won't work in IE as you can't access the type attribute of input in this browser when the node is attached to the DOM tree.

    The best way to implement such input behavior is to create a new input with a different type and replace the current one with it.

    <form>
     <input id="mypass" type="text" name="pass" />
    <input id="sw" type="checkbox" />
    </form>
    
    <script>
    function switchType() {	
      var inp = document.createElement('input'),
           mp = document.getElementById('mypass'),
           sw = document.getElementById('sw');  
      inp.type = (sw.checked) ? 'password' : 'text';
      inp.id = mp.id;
      inp.name = mp.name;
      inp.value = mp.value;
      mp.parentNode.replaceChild(inp, mp);
    }
    
    document.getElementById('sw').onclick = switchType;
    </script>
    Code (markup):
     
    neocoder, Aug 15, 2009 IP