Javascript type change.

Discussion in 'JavaScript' started by ceytimes, May 6, 2011.

  1. #1
    Guys,

    I am new to javascript and I tried to change the type of a input using js.My work works well with Firefox and Chrome but not with IE.IE says

    This is the code I used to change the type.

     var email_type=document.getElementById("email");
        email_type.type='text'; 
    Code (markup):
    Please help me to debug this.
    Thanx.
     
    ceytimes, May 6, 2011 IP
  2. Cash Nebula

    Cash Nebula Peon

    Messages:
    1,197
    Likes Received:
    67
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Yeah, seems that IE doesn't allow you to change the type once the element has been created.
    When you run this in IE, the value attribute changes but the type does not.
    
    <html>
    <head>
    <script type="text/javascript">
    	window.onload = function() {
    		var email_type = document.getElementById("email");
    		email_type.setAttribute('value', 'bar');
    		email_type.setAttribute('type', 'password');
    	}
    </script>
    </head>
    <body>
    <input id="email" type="text" value="foo" />
    </body>
    </html>
    
    Code (markup):
    One way around it is to destroy the element and create a new element with a different type
    
    <html>
    <head>
    <script type="text/javascript">
    	window.onload = function() {
    		var email_type = document.getElementById("email");
    		email_clone = email_type.cloneNode(true);
    		email_clone.setAttribute('type', 'password');
    		document.body.appendChild(email_clone);
    		document.body.removeChild(email_type);
    	}
    </script>
    </head>
    <body>
    <input id="email" type="text" value="foo" />
    </body>
    </html>
    
    Code (markup):
     
    Cash Nebula, May 6, 2011 IP
  3. ceytimes

    ceytimes Peon

    Messages:
    23
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanx buddy it's perfect.



     
    ceytimes, May 6, 2011 IP
  4. mohisha

    mohisha Member

    Messages:
    98
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    26
    #4
    I know core Java very well but i dont know JDBC Connectivity. But i know Ms-Access Connectivity.
     
    mohisha, May 10, 2011 IP
  5. Voynex

    Voynex Peon

    Messages:
    31
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Replacing of the elements will also work:
    oldObject.parentNode.replaceChild(newObject,oldObject);
    Code (markup):
     
    Voynex, May 24, 2011 IP