Results 1 to 10 of 10

Thread: change the type of a field from text to password

  1. #1
    Join Date
    Jan 2007
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy change the type of a field from text to password

    Hi All,

    I would like to change the type of a field from text to password in the onClick event.


    Code:
    function change_type() {
    document.getElementById('Id_Field').type = 'password';
    }

    this code works in Mozilla and Firefox, but Internet Explorer giving error

    pl give me some solution

    Sanoop

  2. #2
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Random guess:
    *might* be that IE doesn't like 'getElementById'... I think there's another thing to use in IE. But I might just be wrong.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  3. #3
    Join Date
    Jan 2007
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    document.testform.testfield.type = 'password';
    also tried problem with type property

  4. #4
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    I doesn't seem to matter how you access the text input (as a part of a form or as a document element or as part of the document.all collection), or whether you use the setAttribute() method or just assign the value directly. IE for some reason just doesn't like this. I found a work around that FF also will happily use:

    Code:
    <html>
    <head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <script type="text/javascript">
    function passit(ip){
    var np=ip.cloneNode(true);
    np.type='password';
    ip.parentNode.replaceChild(np,ip);
    }
    </script>
    </head>
    <body>
    Hi<br><button style="cursor:pointer;" onclick="passit(document.getElementById('test'));">Pword It</button>
    <input id="test" type="text">
    </body>
    </html>
    BTW, since IE 5, IE has been just fine with getElementById.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  5. #5
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Here's another demo with a tweak for browsers that won't clone the value (like Opera, possibly others) that also shows accessing the text input as a part of a form:

    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd">
    <html>
    <head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <script type="text/javascript">
    function passit(ip){
    var np=ip.cloneNode(true);
    np.type='password';
    if(np.value!=ip.value)
    np.value=ip.value;
    ip.parentNode.replaceChild(np,ip);
    }
    </script>
    </head>
    <body>
    <form action="#">
    <input name="pw" id="test" type="text"><br>
    <input type="button" style="cursor:pointer;" onclick="passit(this.form[0]);" value="Pword It">
    <input type="submit" value="submit">
    </form>
    </body>
    </html>
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  6. #6
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

    Default

    IE doesn't support the type attribute when accessed dynamically. I've encountered this problem before myself.
    - Mike

  7. #7
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Quote Originally Posted by mburt View Post
    IE doesn't support the type attribute when accessed dynamically. I've encountered this problem before myself.
    Well, that is obviously not universally true, or else my two little workaround demos wouldn't work in IE but, they do work in IE.

    Also, IE can access it like so:

    <input type="text" onclick="alert(this.type);">
    Last edited by jscheuer1; 02-01-2007 at 01:22 PM. Reason: add info
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  8. #8
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    I seem to remember something about setting it with setAttribute() for IE. Can't remember precisely, though.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  9. #9
    Join Date
    Jul 2006
    Location
    Antwerp, Belgium (Europe)
    Posts
    927
    Thanks
    121
    Thanked 2 Times in 2 Posts

    Default

    And this ?
    Code:
    <script language="JavaScript" type="text/JavaScript">
    function clearDefault(el) {
    if (el.defaultValue==el.value) el.value = ""
    }
    </script>
    
    <INPUT TYPE=TEXT NAME="name" VALUE="x" onFocus="clearDefault(this)" style="border:0px; width:148px">

  10. #10
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Quote Originally Posted by Twey View Post
    I seem to remember something about setting it with setAttribute() for IE. Can't remember precisely, though.
    That works with some things that cannot be accessed directly as attribute/value pairs but, as I said already in this thread, not for this.

    Quote Originally Posted by chechu View Post
    And this ?
    Code:
    <script language="JavaScript" type="text/JavaScript">
    function clearDefault(el) {
    if (el.defaultValue==el.value) el.value = ""
    }
    </script>
    
    <INPUT TYPE=TEXT NAME="name" VALUE="x" onFocus="clearDefault(this)" style="border:0px; width:148px">
    What does that have to do with changing the type attribute?
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •