Ads 468x60px

Pages

Blogroll

Wednesday, December 21, 2011

Watermark using placeholder HTML 5


    <input type="text" placeholder="Enter watermark text" name="txt" />
    <script type="text/javascript">
      
jQuery.support.placeholder = (function () {
    var i = document.createElement('input');
    return 'placeholder' in i;
})();

function WatermarkText() {
    if (!$.support.placeholder) {
        var active = document.activeElement;
        $('[placeholder]').focus(function () {
            if ($(this).attr('placeholder') != undefined && $(this).attr('placeholder') != '' && ($(this).val() == '' || $(this).val() == $(this).attr('placeholder'))) {
                //$(this).val() == '' - check is required here because the field is cleared off placeholder text on form submission
                $(this).val('').removeClass('hasPlaceholder');
            }
        }).blur(function () {
            if ($(this).attr('placeholder') != undefined && $(this).attr('placeholder') != '' && ($(this).val() == '' || $(this).val() == $(this).attr('placeholder'))) {
                $(this).val($(this).attr('placeholder')).addClass('hasPlaceholder');
            }
        });
        $('[placeholder]').blur();
        $(active).focus();
        $('form').submit(function () {
            $(this).find('.hasPlaceholder').each(function () {
                $(this).val('');
                $(this).focus();              
            });

        });
    }
}

    </script>

   <style type="text/css">
        .hasPlaceholder
        {
        font-style:italic;
        }       
    </style>
http://archive.plugins.jquery.com/project/input-placeholder

0 comments:

Post a Comment