Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery input placeholder for type password in IE8

I am using jQuery instead of HTML5 placeholder attribute

<input type="text" name="email" value="Email" onfocus="if (this.value == 'Email') { this.value = ''; }" onblur="if (this.value == '') { this.value = 'Email'; }" onclick="if (this.value == 'Email') { this.value = ''; }"  />

this is working fine for type="text" but for type="password" it show only *

How i will use placeholder for password field? Need out in IE8

Advance thanks for answers...

like image 812
Dhamu Avatar asked Nov 28 '22 17:11

Dhamu


1 Answers

I create a normal input, and on focus switch it to a password field, and if the value is empty on blur, then switch it back to a normal input, else leave it as the password field.

Here is a working JSFiddle: http://jsfiddle.net/q8ajJ/

The HTML

<!-- Style = display none for people who dont have javascript -->
<input type="text" name="fake_pass" id="fake_pass" value="Enter Password:" style="display:none"/>
<input type="password" name="real_pass" id="real_pass"/>​

The Javascript (jQuery)

// On DOM ready, hide the real password
$('#real_pass').hide();

// Show the fake pass (because JS is enabled)
$('#fake_pass').show();

// On focus of the fake password field
$('#fake_pass').focus(function(){
    $(this).hide(); //  hide the fake password input text
    $('#real_pass').show().focus(); // and show the real password input password
});

// On blur of the real pass
$('#real_pass').blur(function(){
    if($(this).val() == ""){ // if the value is empty, 
        $(this).hide(); // hide the real password field
        $('#fake_pass').show(); // show the fake password
    }
    // otherwise, a password has been entered,
    // so do nothing (leave the real password showing)
});

like image 176
Anil Avatar answered Dec 05 '22 01:12

Anil