2

I wanted to use a "self-labelled" input fields to accept username and passwords with javascript as shown below. UX-wise, it seems to be working well. However, since I'm changing the "type" of the password-input field upon focus/blur, I'm concerned if this will pose any kind of risk. It probably should not, but just want to be sure.

<form method="post" action="/login">
    <fieldset>
        <h3>Login</h3>
        <input id="username" name="username" type="text" value="Username" onblur="if (this.value == '') { this.value = 'Username'; }" onfocus="if (this.value == 'Username') { this.value = ''; }"/>
        <input id="password" name="password" type="text" value="Password " onblur="if (this.value == '') { this.value = 'Password '; this.type = 'text' }" onfocus="this.type = 'password'; if (this.value == 'Password ') { this.value = ''; }"/>
        <input id="submit" name="submit" type="submit" value="Login"/>
    </fieldset>
</form>
Zong
  • 6,160
  • 5
  • 32
  • 46
Sterex
  • 1,026
  • 1
  • 13
  • 29
  • 1
    Not an answer to the question, but beware of changing the type of input elements after they have been rendered as it will mess with some browsers. Consider instead showing\hiding different input elements. – beyond-code Aug 02 '13 at 18:33

3 Answers3

1

To do what you want, use input field placeholder:

<input type="password" placeholder="Password">

It will will work just fine in all modern browsers.

Have a look at DEMO in jsFiddle

MightyPork
  • 18,270
  • 10
  • 79
  • 133
1

I would suggest using the placeholder attribute since it does exactly what you are trying to do. There are also jquery/javascript components to emulate what placeholder does in the case of browsers that don't support html5.

doogle
  • 3,376
  • 18
  • 23
1

For modern browser placeholder is perfect.No doubt.

But IE 7,8,9 as always lag.So no support for placeholders when it comes to IE<10.

Solution:This Polyfill will do exactly what you want.And its valid for all browsers.

https://github.com/parndt/jquery-html5-placeholder-shim/

http://www.hagenburger.net/BLOG/HTML5-Input-Placeholder-Fix-With-jQuery.html

Refer:The best placeholder polyfil script for ie7, ie8 and ie9

Community
  • 1
  • 1
HIRA THAKUR
  • 17,189
  • 14
  • 56
  • 87
  • 1
    Thanks for the info! I do not intend to support IE right now. So, placeholder will work for me. However, this will help me in the future. – Sterex Aug 02 '13 at 18:54