473,378 Members | 1,382 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Dynamically change input type TEXT to PASSWORD

kryo
9
Hey Ya'll,

I encountered another issue, with lovely internet explorer... How it likes
to truly **** me off.. :X

Anywho, i'm trying to do this:

Expand|Select|Wrap|Line Numbers
  1. <form>
  2. <input type=TEXT id="some_id">
  3. </form>
  4.  
Expand|Select|Wrap|Line Numbers
  1. input = document.getElementByID('some_id');
  2. alert(input.type);
  3. input.setAttribute('type','password');
  4.  
This works fine, in Mozilla Firefox, but in internet explorer, it complains:
"This command is not supported."

err. Seems so final.. Is there a hack around this? anybody encountered this b4?

Thanks in advance :D
Sep 9 '07 #1
19 63372
kryo
9
... btw, version of Internet Explorer is: 6.0.2900.2180.xpsp_sp2_rtm.040803-2158..

p.s. Isn't that version number just ridiculously long?
Sep 9 '07 #2
mrhoo
428 256MB
Yeah, in IE (through 7) you can't change the type of an input element after it has been added to the document.
You'll have to replace it with a new input element, with its type (and other attributes) already set before being appended.

If you clone the original you can preserve the text value, id and other attributes,
but you MAY need to reset some event handlers.


Expand|Select|Wrap|Line Numbers
  1. var input=document.getElementById(some-id);
  2. var input2= input.cloneNode(false);
  3. input2.type='password';
  4. input.parentNode.replaceChild(input2,input);
Sep 10 '07 #3
kryo
9
Very cool man.. thanks.. I was unaware of the cloneNode and
replaceChild functions.. they will come in handy with some other things
i'm working on.. I'll test this code once i get back to my 'coding lair' :p

Thanks again

Expand|Select|Wrap|Line Numbers
  1. var input=document.getElementById(some-id);
  2. var input2= input.cloneNode(false);
  3. input2.type='password';
  4. input.parentNode.replaceChild(input2,input);
Sep 12 '07 #4
epots9
1,351 Expert 1GB
try this:
Expand|Select|Wrap|Line Numbers
  1. var input = document.getElementByID("id");
  2. input.type = "password"
  3.  
i've used that in ie7 and firefox2, works good.

good luck
Sep 12 '07 #5
kryo
9
Thanks for your post, but unfortunately i'm testing for backwards capability from IE 6+ in IE 6, Msft didnt have that capability... i'm just now testing the above posted solution.. will report my final results, to share :D

try this:
Expand|Select|Wrap|Line Numbers
  1. var input = document.getElementByID("id");
  2. input.type = "password"
  3.  
i've used that in ie7 and firefox2, works good.

good luck
Sep 13 '07 #6
kryo
9
I regret to report that neither of the above solutions work in internet explorer 6... I constantly get the error:

"Could not get the type property. This command is not supported"

The below code seems to work correctly in Firefox 2.0.
However, in Intenet Explorer, it pukes.. Sometimes, it works
the first time w/o puking.. but the 2nd time it will puke for sure..
I'm still trying to come up with some kind of explanation for that.. :D

Expand|Select|Wrap|Line Numbers
  1. function user_set_input_type(input,type)
  2. {
  3.     try
  4.     {
  5.         var input2 = input.cloneNode(false);
  6.         switch(type)
  7.         {
  8.             default:
  9.             case 'text':
  10.             {
  11.                 input2.type = 'text';
  12.                 break;
  13.             }
  14.             case 'password':
  15.             {
  16.                 input2.type = 'password';
  17.                 break;
  18.             }
  19.         }
  20.         input2.id = limput;
  21.         input.parentNode.replaceChild(input2,input);
  22.     }
  23.     catch(e)
  24.     {
  25.         window.status = e.message;
  26.     }
  27. }
  28.  
This code, constantly pukes in I.E 6.0 but still works correctly in FireFox... My guess is that Mozilla allows the type property (which should be read only) to be modified post-runtime.. In this case, i'm glad they do this.. re-creation of the text element would not be optimal in my case. However, I still need to find a solution for I.E. 6... Any help would be greatly appreciated..

Expand|Select|Wrap|Line Numbers
  1. function user_set_input_type(input,type)
  2. {
  3.  
  4.     try
  5.     {
  6.         switch(type)
  7.         {
  8.             default:
  9.             case 'text':
  10.             {
  11.                 input.type = 'text';
  12.                 break;
  13.             }
  14.             case 'password':
  15.             {
  16.                 input.type = 'password';
  17.                 break;
  18.             }
  19.         }
  20.     }
  21.     catch(e)
  22.     {
  23.         window.status = e.message;
  24.     }
  25. }
  26.  
Sep 16 '07 #7
acoder
16,027 Expert Mod 8TB
Does it work on IE7?
Sep 16 '07 #8
pbmods
5,821 Expert 4TB
You may need to use the 'create/replace' method, but generate the input by passing HTML as the parameter to document.createElement():
Expand|Select|Wrap|Line Numbers
  1. document.createElement('<input type="password" ... />');
  2.  
I think that's how you do it in IE. And it should be noted that this ONLY works in IE. If it works.

Not that I have regrets. It's all bright and sunny over here in Mac Land :)
Sep 16 '07 #9
acoder
16,027 Expert Mod 8TB
Remove "default:" from line 8. How are you calling this function?
Sep 16 '07 #10
mrhoo
428 256MB
Expand|Select|Wrap|Line Numbers
  1. function user_set_input_type(input,type){
  2.     try {
  3.         var input2 = input.cloneNode(false);
  4.         switch(type){
  5.             default:
  6.             case 'text': {
  7.                 input2.type  = 'text';
The local variable type is overriding the property name 'type' here
Sep 17 '07 #11
I wanted to post a reply here because I found this thread as a result of searching to find an answer to the same problem. This thread pointed me in the direction I finally went in and this worked (I'm writing an HTA so if it works there it should work in IE.) Also note I'm using the wonderfully inflexible VBScript to get this one to work. Javascript should be similar or even easier...

Expand|Select|Wrap|Line Numbers
  1. Function fncShowPass()
  2.    Dim oElement, oNewElement, strType
  3.    Set oElement = document.getElementByID("passwordfield")
  4.    'I am using a checkbox to hide/show the password
  5.    If document.boxhidden.checked then
  6.       strType = "text"
  7.    else
  8.       strType = "password"
  9.    end if
  10.    Set oNewElement = document.createElement("<input disabled id=passwordfield type=" & strType &_
  11.       " value=" & oElement.value & " />")
  12.    call oElement.replaceNode(oNewElement)
  13. End Function
Sep 26 '07 #12
acoder
16,027 Expert Mod 8TB
Welcome to TSDN!

Thanks for posting your solution.
Also note I'm using the wonderfully inflexible VBScript to get this one to work. Javascript should be similar or even easier...

Expand|Select|Wrap|Line Numbers
  1. Function fncShowPass()
  2.    Dim oElement, oNewElement, strType
  3.    Set oElement = document.getElementByID("passwordfield")
  4.    'I am using a checkbox to hide/show the password
  5.    If document.boxhidden.checked then
  6.       strType = "text"
  7.    else
  8.       strType = "password"
  9.    end if
  10.    Set oNewElement = document.createElement("<input disabled id=passwordfield type=" & strType &_
  11.       " value=" & oElement.value & " />")
  12.    call oElement.replaceNode(oNewElement)
  13. End Function
Converting this to Javascript would be relatively easy, but instead of using the full HTML with document.createElement (which is incorrect syntax), the cloneNode/replaceChild method should work.
Sep 27 '07 #13
I think it's better to use a safe solution for this, having 2 inputs, one text and one password and switch the "display" property for them.
the HTML:
Expand|Select|Wrap|Line Numbers
  1. <form>
  2. <input type="text" id="passwordtext" value="Password" onclick="switchto(1)" onkeydown="switchto(1)">
  3. <input type="password" id="password" value="" onblur="if (this.value=='')switchto(0)" style="display:none">
  4. </form>
the JS:
Expand|Select|Wrap|Line Numbers
  1. function switchto(q){
  2.     if (q){
  3.         document.getElementById('passwordtext').style.display="none";
  4.         document.getElementById('password').style.display="inline";
  5.         document.getElementById('password').focus();
  6.     } else {
  7.         document.getElementById('password').style.display="none";
  8.         document.getElementById('passwordtext').style.display="inline";
  9.     }
  10. }
// the code can be optimized, but you get my point
Sep 28 '07 #14
pbmods
5,821 Expert 4TB
Heya, Paputza. Welcome to TSDN!

Please use CODE tags when posting source code:

[CODE=javascript]
JavaScript code goes here.
[/CODE]
Sep 28 '07 #15
I had this same problem, here is how I got around it (Mine is written in prototype so here is the pseudo-code):

1 text input (id:"password_fake", default value:"Password")
1 password input ("password")

NOTE: elements must be of the same exact style and sequential in the DOM.

Event listener for "password_fake":
Onfocus
- hide "password_fake"
- show "password"
- focus on "password"

Event listener for "password"
Onblur
- if "password".value.length == 0 then:
- hide "password"
- show "password_fake"

works on FF, IE7,IE6
Mar 25 '09 #16
Hi all, I got the same issue with IE8 , I took the code that Flash13 published and I modified it :
Expand|Select|Wrap|Line Numbers
  1. function RepType(){
  2.     var input = document.getElementById(ElementID'); 
  3.     var input2 = document.createElement('sTag);
  4.     with (input2){
  5.         id = input.id;
  6.         value = input.value;
  7.         type = 'password';
  8.     }
  9.     input.parentNode.replaceChild(input2,input);         
  10. }
  11.  
Work like a charme
Aug 8 '09 #17
gits
5,390 Expert Mod 4TB
just a note: it is not recommended to use the with-statement have a look here and here ...

kind regards
Aug 8 '09 #18
This is my way...
Expand|Select|Wrap|Line Numbers
  1. <input id="paypal_cc_number" name="paypal_cc_number[]" type="hidden">
  2. <input id="texto_paypal_cc_number" onblur="enmascarar()" onfocus="desenmascarar()"  style="display:block">
Expand|Select|Wrap|Line Numbers
  1. <script>
  2. function enmascarar() 
  3. {
  4.     document.getElementById("paypal_cc_number").value = document.getElementById("texto_paypal_cc_number").value;
  5.     if(document.getElementById("texto_paypal_cc_number").value!="")
  6.         document.getElementById("texto_paypal_cc_number").value = "**************";
  7.  }
  8.  
  9. function desenmascarar() 
  10. {
  11.     document.getElementById("texto_paypal_cc_number").value = document.getElementById("paypal_cc_number").value;
  12. }
  13.  
and it works in IE 6 and 7 and chrome and firefox!!!

enjoy it!
Aug 5 '10 #19
I am agreed with "paputza" I have used two type of fields text and password. password status is none but when you clicks on text field i display password control and hide text control. If we go for other solution we will keep on facing cross browsers issues :)
Oct 19 '10 #20

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: sebastien | last post by:
Hi, What does this always jumps to another page when I try to asign text in the input type text : titel <form id="formie" name="formie" method="post" action="/staff/news/news.php"> <input...
3
by: Chris John Jordan | last post by:
How can one get a text control's size to fill the available space e.g. the width of the cell? I find not input type=text size=100% .. Thanks. -- Chris
3
by: Vanitha | last post by:
Hi All, The following syntax is not working on Mozilla browser, however this works on IE. <input id="freq" size="20" name="freq" type="text"> When i refer this freq from a javascript...
2
by: Hoss | last post by:
Hi guys, The framework of my page is an aspx page with a header and a footer. The header is a menu system and depending on what you select there different ascx pages are loaded into the middle...
8
by: Gianni Rondinini | last post by:
hi all. i've almost finished moving all the tags from html to my css. however, i can't find a way (if there's one) to move the property maxlength of an <input type=text ...tag to the css in...
2
by: babanner | last post by:
echo " <input type=\"text\" id=\"inputnumber\" value=\"\" /> <input type=\"button\" value=\"Go\" onclick=\"javascript:window.location='". $_SERVER ....
10
by: test9991014 | last post by:
Hi all, I have an <input type=texttag that is taking up too much space vertically. It's in a <tdthat has a fixed height, and I've set the height value in the <input>'s style to be 10px, less...
7
by: Jack Gray | last post by:
I have a form requiring data input for all fields. When any field is left blank and the data is submitted, the cgi file generates a new form which is populated with data already input and an error...
2
by: AlexanderDeLarge | last post by:
Hi! I got a problem that's driving me crazy and I'm desperately in need of help. I'll explain my scenario: I'm doing a database driven site for a band, I got these tables for their discography...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.