SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Wizard geiger's Avatar
    Join Date
    Jul 2001
    Posts
    2,459
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Changing the 'type' attribute of an input tag

    Is it possible to change the 'type' attribute of an input tag via javascript? Like, say I have a text input box and a form button. Can I change the text input box to a password input box when I hit the button (retaining the same contents, etc)? Or perhaps only when it's held? Seems like an odd question, but can it be done?

    Thanks.

  2. #2
    SitePoint Enthusiast RogueJedi's Avatar
    Join Date
    Oct 2001
    Location
    Texas
    Posts
    96
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The code

    Code:
    <script type="text/javascript">
    
    function changType(elm) {
        theTag = document.getElementById(elm);
        if(theTag.type == "text") 
            theTag.type = "password";
        return true;
    }
    </script>
    
    <form>
    <input type="text" id="textbox" value="something" maxsize="20" size="20" /><br />
    <input type="submit" id="submit" value="submit" onclick="changType('textbox')" />
    </form>
    produced the error message "Line 16, char 9, Could not get the type property. This command is not supported."

    Based on that, I'd say what you want to do is not possible. The type property is probably read only and therefore unchangeable.

    Edit: From the MSDN Library page on the type property:
    As of Microsoft® Internet Explorer 5, the type property is read/write-once, but only when an input element is created with the createElement method and before it is added to the document.
    So, it the type property can be changed, but only before it's put into the page, and that may only be in IE. I don't know about Mozilla/Netscape.
    Last edited by RogueJedi; Feb 23, 2003 at 13:34.
    Rob Nolan
    Do or do not, there is no try.

  3. #3
    SitePoint Wizard geiger's Avatar
    Join Date
    Jul 2001
    Posts
    2,459
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hmmm okay. Thanks anyway.


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
  •