2

I have implemented the following scripts to display placeholders in IE for inputbox,but it is not working for password field ,Please advice me..

<script>
 jQuery(function() {
    jQuery.support.placeholder = false;
    test = document.createElement('input');
    if('placeholder' in test) jQuery.support.placeholder = true;
    });

    // Placeholder for IE
    $(function () {
        if(!$.support.placeholder) { 

            var active = document.activeElement;
            $(':text, textarea').focus(function () {
                if ($(this).attr('placeholder') != '' && $(this).val() == $(this).attr('placeholder')) {
                    $(this).val('').removeClass('hasPlaceholder');
                }
            }).blur(function () {
                if ($(this).attr('placeholder') != '' && ($(this).val() == '' || $(this).val() == $(this).attr('placeholder'))) {
                    $(this).val($(this).attr('placeholder')).addClass('hasPlaceholder');
                }
            });
            $(':text, textarea').blur();
            $(active).focus();
            $('form').submit(function () {
                $(this).find('.hasPlaceholder').each(function() { $(this).val(''); });
            });
        }  

    });
</script>
Naju
  • 1,541
  • 7
  • 27
  • 59

2 Answers2

2

Try to add input[type="password"] to your selector, so change:

$(':text, textarea')

to:

$(':text, textarea, input[type="password"]')

since :text selector only select <input type="text"> elements

Felix
  • 37,892
  • 8
  • 43
  • 55
2

Two issues:

  1. You're not selecting password fields. The :text jQuery selector only selects inputs that are type=text (either explicitly or implicitly). You'd have to add input[type=passsword] to your selector to also include password fields.

  2. But once you fix that, you'll have the problem that, of course, the password field's value is displayed obscured. So setting your placeholder text on it won't show the placeholder. You'll have to show the placeholder text some other way, such as a span or div that you overlay on top of the field.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875