Question

how to change the input type dynamically

Asked by: hart

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

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2003-12-03 at 23:00:04ID20816069
Tags

input

,

change

,

type

,

dynamically

Topic

JavaScript

Participating Experts
7
Points
500
Comments
33

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. Same onFocus function for all input textbox
    how do I give all the input textboxs the same onFocus function?
  2. Crazy onblur - onfocus - onsubmit problem
    Hello, I am working on a project for a customer's e-commerce site. In particular, I am working on the product pages. Before I ask my question, let me give you a little detail about what I'm dealing with... The product page that I'm working on can have 1 or more products (i...
  3. Onfocus and Onblur
    I'm trying to use OnFocus and Onblur to focus on a textbox when I click in the field. When I click in the textbox the value for the textbox disappears so I can type what I want to enter. The only problem is if I don't type anything in the textbox and the value is displayed,...
  4. Onfocus errors
    I am having an issue where when I click on the text box the original text dissapears, and if I click off the textarea and then back to it, all the text dispears. This is on a form that needs to be sumbmitted, so the text needs to display. I tried looking up similar questions,...

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: sajuksPosted on 2003-12-03 at 23:09:10ID: 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">

 

by: ho_alanPosted on 2003-12-03 at 23:16:28ID: 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>

 

by: hartPosted on 2003-12-03 at 23:22:24ID: 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

 

by: ho_alanPosted on 2003-12-03 at 23:24:14ID: 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>

 

by: sajuksPosted on 2003-12-03 at 23:27:24ID: 9872804

oops!!!!!!!!
wouldnt a hidden field solve your problem ?
store the passsword in it and interact with it for your db requirements?

 

by: ho_alanPosted on 2003-12-03 at 23:47:58ID: 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

 

by: hartPosted on 2003-12-03 at 23:57:11ID: 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

 

by: ho_alanPosted on 2003-12-04 at 00:02:33ID: 9872910

does the code in my latest post work?

 

by: sajuksPosted on 2003-12-04 at 00:21:08ID: 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>

 

by: mplungjanPosted on 2003-12-04 at 00:35:41ID: 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

 

by: sajuksPosted on 2003-12-04 at 00:49:02ID: 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 .



 

by: mplungjanPosted on 2003-12-04 at 00:52:19ID: 9873059

Yes, the code I provide is barebones. No test for backspace and IE only (due to the window.event)

 

by: hartPosted on 2003-12-04 at 01:06:43ID: 9873094

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

Regards
Hart

 

by: hartPosted on 2003-12-04 at 01:11:57ID: 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

 

by: hartPosted on 2003-12-04 at 01:15:13ID: 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

 

by: NetGroovePosted on 2003-12-04 at 01:26:08ID: 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>



 

by: NetGroovePosted on 2003-12-04 at 01:56:23ID: 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>



 

by: devicPosted on 2003-12-04 at 01:59:35ID: 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>

 

by: hartPosted on 2003-12-04 at 02:10:08ID: 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

 

by: hartPosted on 2003-12-04 at 02:16:06ID: 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

 

by: devicPosted on 2003-12-04 at 02:17:43ID: 9873336

impossible

 

by: Christian_WenzPosted on 2003-12-04 at 02:32:21ID: 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.

 

by: NetGroovePosted on 2003-12-04 at 02:35:15ID: 9873393

What Netscape are we talking here about?

 

by: hartPosted on 2003-12-04 at 02:40:15ID: 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

 

by: mplungjanPosted on 2003-12-04 at 02:43:23ID: 9873428

HArt:
<input type="text" onFocus="this.type='password'">
gives an error so the answer is no

 

by: hartPosted on 2003-12-04 at 03:00:36ID: 9873483

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

Regards
Hart

 

by: mplungjanPosted on 2003-12-04 at 04:46:34ID: 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

 

by: hartPosted on 2003-12-04 at 04:53:14ID: 9874015

thanks mate :-)

regards
hart

 

by: orangechickenPosted on 2004-01-15 at 11:46:11ID: 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
                                    

 

by: Christian_WenzPosted on 2004-01-19 at 10:24:19ID: 10148624

fyi: it won't work with NN4 :(

 

by: orangechickenPosted on 2004-01-19 at 20:40:14ID: 10152504

Developers should refuse to support NN4 anyway. We should be supporting NN6.2 and 7. NN4 is sooooo 90s. ;)

 

by: mplungjanPosted on 2004-01-20 at 10:19:08ID: 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

 

by: mplungjanPosted on 2004-01-20 at 10:19:42ID: 10157032

PS: How about refusing to work on less than Mozilla 1.4 and IE6?

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...