Solved

how to change the input type dynamically

Posted on 2003-12-03
33
2,052 Views
Last Modified: 2007-12-19
hi experts,

i have a very small query, how do i change the attribute type of a input type, what i mean is this

initially i have a input type in the form

<input type="text" name="userpassword" value="enter password">

now on focus of this text box i want to change the type="text" to type="password"
i.e something like this, but i don't know wether its possible or not


<input type="text" name="userpassword" value="enter password" onFocus="this.Type='password';this.value='';">

the above code doesn't give any error, but also my requirement is not solved.
the data enetered is shown as text and not in pasowrd format...

Regards
Hart
0
Comment
Question by:hart
  • 10
  • 6
  • 4
  • +5
33 Comments
 
LVL 33

Expert Comment

by:sajuks
ID: 9872741
I dont think thats possible, a better way might be to encrypt the data as you enter in the textfield.
or else
<input type="text" name="user_name"  value = "enter user name">
<input type="password" name="password" value = "enter passwd">
0
 
LVL 5

Expert Comment

by:ho_alan
ID: 9872760
here is an example
but there may be some simpler methods
hope this can inspire u

<html>
<script>
var password = ""
function mask()
{
   password=document.form1.pwd.value
   tmp=""
   for (i=0; i < password.length; i++)
       tmp=tmp+"*"
   document.form1.pwd.value = tmp
}
function unmask()
{
  document.form1.pwd.value = password
}
</script>
<body>
<form name=form1>
<input type=text name=pwd onchange="mask()" onfocus="unmask()">
</form>
</body>
</html>
0
 
LVL 11

Author Comment

by:hart
ID: 9872783
sajuks : if it was that simple i wouldn't have asked only..., i know what type password is.

see my requirement is that i don't have any label besides the password field..
so i show enter password in the start, when the user puts his focus on the field i make the value null and then the type should change to password...

ho_alan : ur solution will work.., but the problem is when i will interact with the database then the value of the userpassword field would be just ****, hope u get what i am saying..

Regards
Hart
0
 
LVL 5

Expert Comment

by:ho_alan
ID: 9872792
should use "onBlur"
<html>
<script>
var password = ""
function mask()
{
   password=document.form1.userpassword.value
   tmp=""
   for (i=0; i < password.length; i++)
       tmp=tmp+"*"
   document.form1.userpassword.value = tmp
}
function unmask()
{
  document.form1.userpassword.value = password
}
</script>
<body>
<form name=form1>
<input type=text name="userpassword" value="enter password" onBlur="mask()" onfocus="unmask()">
</form>
</body>
</html>
0
 
LVL 33

Expert Comment

by:sajuks
ID: 9872804
oops!!!!!!!!
wouldnt a hidden field solve your problem ?
store the passsword in it and interact with it for your db requirements?
0
 
LVL 5

Assisted Solution

by:ho_alan
ho_alan earned 75 total points
ID: 9872879
ok, a little bit update (inspired by sajuks :-) )

<html>
<script>
var password = ""
function mask()
{
  password=document.form1.userpassword.value
  tmp=""
  for (i=0; i < password.length; i++)
      tmp=tmp+"*"
 document.form1.realpassword.value = password
  document.form1.userpassword.value = tmp
}
function unmask()
{
 document.form1.userpassword.value = password
 document.form1.realpassword.value = password
}
</script>
<body>
<form name=form1>
<input type=text name="userpassword" value="enter password" onBlur="mask()" onfocus="unmask()">
<input type=hidden name="realpassword">
</form>
</body>
</html>

or u can assign the value of realpassword just before u submit the form
0
 
LVL 11

Author Comment

by:hart
ID: 9872900
ok can u guys give me a code, as soon as the user eneters something in the text box, that should change to * and the original thing that was written by the user is appended in an hidden field.

i will split the points and close this q, if u give it a.s.a.p ;-)

Regards
Hart
0
 
LVL 5

Expert Comment

by:ho_alan
ID: 9872910
does the code in my latest post work?
0
 
LVL 33

Assisted Solution

by:sajuks
sajuks earned 50 total points
ID: 9872958
try this example, another modfcn of   ho_alan's script
--------------------------------------------------------------------------------------------------------------------------------------------------------------

<html>
<script>
var password = ""
function mask()
{
password=document.form1.userpassword.value
tmp=""
for (i=0; i < password.length; i++)
tmp=tmp+"*"
//document.form1.realpassword.value = password
document.form1.userpassword.value = tmp
}
</script>
<body>
<form name=form1 onSubmit = "alert (document.form1.realpassword.value)" >
<input type=text name="userpassword" value="enter password" onblur="mask()"  onkeyup= "realpassword.value = this.value">
<input type=hidden name="realpassword">
<input type="submit">

</form>
</body>
</html>

0
 
LVL 75

Assisted Solution

by:Michel Plungjan
Michel Plungjan earned 75 total points
ID: 9873009
<form>
<input type="text" onFocus="this.val1=''; this.val2=''"
onKeyUp="key = event.keyCode;
this.val1+='*';
this.value=val1;
this.val2+=String.fromCharCode(key);
this.form.f2.value=this.val2">
<input type="text" name="f2"></form>

Without further changes it will send uppercase value. BUt that COULD be modified
0
 
LVL 33

Expert Comment

by:sajuks
ID: 9873048
In mplungjan code, if the user selects backspace,delete function wouldnt that too get trapped ?
then the code would have to include checking for these control buttons also.
rite ?
am not saying that the other code is better or faster ,am just trying to learn javascript by looking into the scripts that you people write .



0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 9873059
Yes, the code I provide is barebones. No test for backspace and IE only (due to the window.event)
0
 
LVL 11

Author Comment

by:hart
ID: 9873094
mplungjan : just to be sure there is no other way of directly changing the type="text" to type="password" through javascript :-)

Regards
Hart
0
 
LVL 11

Author Comment

by:hart
ID: 9873112
and just to let sajuks and ho_alan's know

their code only change the text to * when i blur from he field, where as i wanted it to happen while i type..
also.. the hidden field will have **alphabet if i use ur code...

i know that u both have put ur efforts into this thread, i can understand bcos i too am an expert on this site...
but i need a concrete solution that will work accross browsers....

Regards
Hart
0
 
LVL 11

Author Comment

by:hart
ID: 9873121
i know that i can just put in the labels on the side and make it type="password" as normal password type boxes are
but then its no fun doing the normal stuff ;-)

anywayz thank u all for the support so far...

Regards
Hart
0
 
LVL 10

Expert Comment

by:NetGroove
ID: 9873162
I could not resist :)

Is this what you need:

<html>
<head>
<script>
function showPwd(theField){
  theField.parentNode.style.display='none';
  pwdField = theField.form.Password;
  pwdField.parentNode.style.display='';
  pwdField.focus();
}
function showNote(theField){
  if(theField.value==""){
    theField.parentNode.style.display='none';
    noteField = theField.form.pwdNote;
    noteField.parentNode.style.display='';
  }
}
</script>
</head>
<body>
<form>
<div>
<input type=text name="pwdNote" value="Enter Password" onFocus="showPwd(this)"></div>
<div style="display:none">
<input type=password name="Password" onBlur="showNote(this)"></div>
</form>
</body>
</html>



0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 10

Accepted Solution

by:
NetGroove earned 250 total points
ID: 9873256
Sorry, no div neccessary.

Take this:
<html>
<head>
<script>
function showPwd(theField){
  theField.style.display='none';
  pwdField = theField.form.Password;
  pwdField.style.display='block';
  pwdField.focus();
}
function showNote(theField){
  if(theField.value==""){
    theField.style.display='none';
    noteField = theField.form.pwdNote;
    noteField.style.display='';
  }
}
</script>
</head>
<body>
<form>
<input type=text name="pwdNote" value="Enter Password" onFocus="showPwd(this)">
<input type=password name="Password" style="display:none" onBlur="showNote(this)">
</form>
</body>
</html>



0
 
LVL 25

Assisted Solution

by:devic
devic earned 50 total points
ID: 9873268
hi hart,

here are my two examples:

one:
============================================
<form name=myform>
<input name=textfield type=text value=devic>
<input name=other type=text value=devic>
</form>
<script>
function runit()
{
    formHTML =document.myform.innerHTML.split("<");
      for(var i=0;i<formHTML.length;i++)
      {
            if(formHTML[i].match(/textfield/))
            {
                  formHTML[i]="input name=textfield type=password value=devic>"
            }
      }
      document.myform.innerHTML=formHTML.join("<")
}
</script>
<button onclick=runit()>runit()</button>


two
=================================

<form name=myform>
<input name=textfield type=text value=devic>
</form>
<script>
function runit()
{
      document.myform.textfield.outerHTML="<input type=password name=textfieldsasa value="+document.myform.textfield.value+">"
}
</script>
<button onclick=runit()>runit()</button>
0
 
LVL 11

Author Comment

by:hart
ID: 9873311
sorry devic, but i don't want innerHtml stuff...

NetGroove cool stuff, but its not compatible in netscape....
may be u could do something about that and i am done ;-)

Regards
Hart
0
 
LVL 11

Author Comment

by:hart
ID: 9873333
but i don' get one thing, can't we dynamically change the input type attribute to password in javacript...

no ones answering that... plz do say ur opinions about that 2..

Regards
Hart
0
 
LVL 25

Expert Comment

by:devic
ID: 9873336
impossible
0
 
LVL 5

Expert Comment

by:Christian_Wenz
ID: 9873382
impossible, I agree.

The solution I like best is the one with the mask() function and the hidden field. However instead of onchange="mask()", I recommend onkeydown="mask()"; then, of course, the mask() function has to check what is new.
If Netscape 4 compatibility is an issue, just set a timeout to call the extended mask() function every second or so.

0
 
LVL 10

Expert Comment

by:NetGroove
ID: 9873393
What Netscape are we talking here about?
0
 
LVL 11

Author Comment

by:hart
ID: 9873417
hope i was fair enough in splitting the points, if not then do forgive this fellow expert .. ;-)

NetGroove : I need it to be compatible with NS 4.7 and 7

its upto u, if u can do it and give, then its fine, else i will solve it later..


Thanks a lot all of u ;-)

Regards
Hart
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 9873428
HArt:
<input type="text" onFocus="this.type='password'">
gives an error so the answer is no
0
 
LVL 11

Author Comment

by:hart
ID: 9873483
mplungjan : try <input type="text" onFocus="this.Type='password'">

Regards
Hart
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 9873988
Yes, that will create the new attribute called "Type" which is not a known attribute. It will also do nothing else.

document.forms[0].elements[x].type is lowercase and read only
http://msdn.microsoft.com/workshop/author/dhtml/reference/properties/type_7.asp

innerHTML works in Mozilla and possibly in NS6
None of the hide/reveal or change content of cells/divs/tags will work in NS4

Michel
0
 
LVL 11

Author Comment

by:hart
ID: 9874015
thanks mate :-)

regards
hart
0
 
LVL 1

Expert Comment

by:orangechicken
ID: 10123275
Here's a really good way I just came up with using CSS and Javascript.

NOTE: NS4.x probably won't work (but I don't have it installed (because I refuse to develop for it) so I didn't test it)

Here's the sample page:

<html>
      <head>
            <title></title>

            <style type="text/css">
                  input.username, input.password {
                        width: 200px; /* whatever style you want */
                  }
                  input.password.hidden {
                        display: none;
                        position: relative;
                        top: -19px;           /* do testing on this to find the right numbers for your site and style sheet */
                        margin-bottom: -19px; /* the relative content, when displayed, adds vertical space. Test it on your site. */
                  }
            </style>
            <script language="javascript" type="text/javascript">
                  function switchPasswordField( inputField ){
                        var login_form = document.getElementById( "login_form" );
                        inputField.style.display='none';
                        login_form.password.style.display='block';
                        login_form.password.focus();
                  }
            </script>

      </head>
      <body>

            <form id='login_form'>
                  <input type="text" name="username" value="Type your name" class="username" onclick="this.value=''" /><br />
                  <input type="text" name="switcher" value="Type your password" class="password" onclick="switchPasswordField(this);" /><br />
                  <input type="password" name="password" value="" class="password hidden" />
            </form>

            Test text for margins.

      </body>
</html>

Important to note:
1. Your server-side script would use the value of the 'password' field and not 'switcher'. 'switcher' is just there to display our 'type your password' message.
2. Your server-side script could set the value in both switcher and password if you needed to resubmit the data.
3. Test the values in the style sheet to get it to work for your site and stylesheet.


Good luck to you and YMMV! (I tested in Mozilla .7 and IE6)
-chicken
                                    
0
 
LVL 5

Expert Comment

by:Christian_Wenz
ID: 10148624
fyi: it won't work with NN4 :(
0
 
LVL 1

Expert Comment

by:orangechicken
ID: 10152504
Developers should refuse to support NN4 anyway. We should be supporting NN6.2 and 7. NN4 is sooooo 90s. ;)
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 10157029
Refuse is a grand word. Some of us work for coorporations that need to support countries that may not have the hardware to run the latest browsers. If it is not a big deal, degrading gracefully is so much friendlier than not work at all.
Also more and more people will run on WebTV and handhelds...
Michel
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 10157032
PS: How about refusing to work on less than Mozilla 1.4 and IE6?
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Today I would like to talk about localizing (Internationalization) JavaScript applications. Introduction When creating an application that is going to be used by many people around the globe, it is important to remember that not everyone speak…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now