Results 1 to 8 of 8

Thread: change item type in java script

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

    Default change item type in java script

    Dear friends,

    Is it possible to change item type in java script. for example change type text to password.

    Thanks
    Sanoop

  2. #2
    Join Date
    Sep 2006
    Location
    St. George, UT
    Posts
    2,769
    Thanks
    3
    Thanked 157 Times in 155 Posts

    Default

    Try the following, just edit out what you need to.

    Code:
    <html>
    <head>
    <script type="text/javascript">
    
    function changeField() {
    var field = document.testForm.textField;
    
      if (field.type == "text") {
         test = false;
         field.type = "password";
      }
    
      else {
         field.type = "text"; 
      }
    
    }
    </script>
    </head>
    <body>
    <form name="testForm">
    
    <input type="" name="textField">
    </form>
    
    <a href="#" onclick="changeField(); return false">Change Field	</a>
    </body>
    </html>
    Hope this helps.

    Added Later: There's probably a much better way of doing this, but I'm not sure. The above script has been tested in FF and it works.
    "Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
    TheUnlimitedHost | The Testing Site | Southern Utah Web Hosting and Design

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

    Unhappy

    Thank for your repaly.

    there is an error while running this code "could not get the type property" at line no 10(field.type = "password").

    document.testForm.textField.type = "text" also tried

  4. #4
    Join Date
    May 2006
    Location
    Sydney, Australia - Near the coast.
    Posts
    1,995
    Thanks
    0
    Thanked 8 Times in 7 Posts

    Default

    Slight Modifications:

    HTML Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <title>Change Field</title>
    <script type="text/javascript">
    
    function changeField() {
    var field = document.forms['testForm'].elements['textField'];
    
      if (field.getAttribute("type") == "text") {
         field.setAttribute("type","password");
      }
    
      else {
         field.setAttribute("type","text"); 
      }
    
    }
    </script>
    </head>
    
    <body>
    <form name="testForm">
    
    <input type="text" name="textField">
    </form>
    
    <a href="javascript:changeField()">Change Field	</a>
    </body>
    </html>
    Should work. Untested.
    Last edited by tech_support; 01-30-2007 at 03:34 AM.
    Peter - alotofstuffhere[dot]com - Email Me - Donate via PayPal - Got spare hardware? Donate 'em to me :) Just send me a PM.
    Currently: enjoying the early holidays :)
    Read before posting: FAQ | What you CAN'T do with JavaScript | Form Rules | Thread Title Naming Guide

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

    Default

    this code also not working in IE. field.setAttribute function is not supporting in IE

  6. #6
    Join Date
    May 2006
    Location
    Sydney, Australia - Near the coast.
    Posts
    1,995
    Thanks
    0
    Thanked 8 Times in 7 Posts

    Default

    Well, time to use another method:

    HTML Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <title>Change Field</title>
    <script type="text/javascript">
    var switcher = 0;
    function changeField() {
    var textfield = document.forms['testForm'].elements['textField'];
    var passfield = document.forms['testForm'].elements['passField'];
    
      if (switcher == 0) {
         textfield.style.display = "none"
    	 passfield.value = textfield.value
    	 passfield.style.display = "block"
    	 switcher++
      }
    
      else {
         textfield.style.display = "block"
    	 textfield.value = passfield.value
    	 passfield.style.display = "none"
    	 switcher--
      }
    
    }
    </script>
    </head>
    
    <body>
    <form name="testForm">
    
    <input type="text" name="textField">
    <input type="password" name="passField" style="display:none">
    </form>
    
    <a href="javascript:changeField()">Change Field	</a>
    </body>
    </html>
    Peter - alotofstuffhere[dot]com - Email Me - Donate via PayPal - Got spare hardware? Donate 'em to me :) Just send me a PM.
    Currently: enjoying the early holidays :)
    Read before posting: FAQ | What you CAN'T do with JavaScript | Form Rules | Thread Title Naming Guide

  7. #7
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    29,000
    Thanks
    44
    Thanked 3,198 Times in 3,160 Posts
    Blog Entries
    12

  8. #8
    Join Date
    May 2006
    Location
    Sydney, Australia - Near the coast.
    Posts
    1,995
    Thanks
    0
    Thanked 8 Times in 7 Posts

    Default

    Oh, so this is basically a cross-post.
    Peter - alotofstuffhere[dot]com - Email Me - Donate via PayPal - Got spare hardware? Donate 'em to me :) Just send me a PM.
    Currently: enjoying the early holidays :)
    Read before posting: FAQ | What you CAN'T do with JavaScript | Form Rules | Thread Title Naming Guide

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
  •