how to change the input type dynamically

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
LVL 11
hartAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

sajuksCommented:
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
ho_alanCommented:
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
hartAuthor Commented:
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
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

ho_alanCommented:
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
sajuksCommented:
oops!!!!!!!!
wouldnt a hidden field solve your problem ?
store the passsword in it and interact with it for your db requirements?
0
ho_alanCommented:
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
hartAuthor Commented:
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
ho_alanCommented:
does the code in my latest post work?
0
sajuksCommented:
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
Michel PlungjanIT ExpertCommented:
<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
sajuksCommented:
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
Michel PlungjanIT ExpertCommented:
Yes, the code I provide is barebones. No test for backspace and IE only (due to the window.event)
0
hartAuthor Commented:
mplungjan : just to be sure there is no other way of directly changing the type="text" to type="password" through javascript :-)

Regards
Hart
0
hartAuthor Commented:
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
hartAuthor Commented:
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
NetGrooveCommented:
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
NetGrooveCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
devicCommented:
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
hartAuthor Commented:
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
hartAuthor Commented:
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
devicCommented:
impossible
0
Christian_WenzCommented:
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
NetGrooveCommented:
What Netscape are we talking here about?
0
hartAuthor Commented:
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
Michel PlungjanIT ExpertCommented:
HArt:
<input type="text" onFocus="this.type='password'">
gives an error so the answer is no
0
hartAuthor Commented:
mplungjan : try <input type="text" onFocus="this.Type='password'">

Regards
Hart
0
Michel PlungjanIT ExpertCommented:
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
hartAuthor Commented:
thanks mate :-)

regards
hart
0
orangechickenCommented:
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
Christian_WenzCommented:
fyi: it won't work with NN4 :(
0
orangechickenCommented:
Developers should refuse to support NN4 anyway. We should be supporting NN6.2 and 7. NN4 is sooooo 90s. ;)
0
Michel PlungjanIT ExpertCommented:
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
Michel PlungjanIT ExpertCommented:
PS: How about refusing to work on less than Mozilla 1.4 and IE6?
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.