[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1302
  • Last Modified:

Text field -> password field -> text field?

In order to have a password input field that has the defaul text "Enter Password:", I'm using the code below (found elsewhere in this forum).  But, how do you restore the default text / revert back to regular text field if the user clicks out of the box without entering anything?

So, it goes:

Text field : Default text = "Enter password"
User clicks in the text field and it becomes a password input box
User clicks out of the field without entering anything, and it goes back to a regular text field with the default "Enter password:" text

Thanks


<div id="pwTSpan">
<input id="pwx" type="text" onFocus="document.getElementById('pwTSpan').style.display='none'; document.getElementById('pwSpan').style.display=''; this.form.pw.focus()" value="password">
</div>
 
<div id="pwSpan" style="display:none"><input name="pw" id="pw" type="password" value=""></div>

Open in new window

0
n00b0101
Asked:
n00b0101
  • 3
1 Solution
 
hujiCommented:
It is not possible, becasue in order to see if the user has left the password field blank, you should check the value of that field, and JavaScript doesn't allow you to check the value of password fields.
0
 
hujiCommented:
Oops! Ignore my last! Javasciprt "allows" checking the value. So all you need to do is something like:

<div id="pwSpan" style="display:none"><input name="pw" id="pw" type="password" value="" onBlur="if (document.getElementById('pw').value==''){ document.getElementById('pwSpan').style.display='none'; document.getElementById('pwTSpan').style.display=''; }"></div>

0
 
hujiCommented:
This is the final version of the code which works on all browsers:

<div id="pwTSpan">
<input id="pwx" type="text" onFocus="document.getElementById('pwTSpan').style.display='none'; document.getElementById('pwSpan').style.display=''; document.getElementById('pw').focus()" value="password">
</div>
 
<div id="pwSpan" style="display:none"><input name="pw" id="pw" type="password" value="" onBlur="if (document.getElementById('pw').value==''){ document.getElementById('pwSpan').style.display='none'; document.getElementById('pwTSpan').style.display=''; }"></div>
0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now