Created
May 24, 2012 12:30
-
-
Save roose/2781312 to your computer and use it in GitHub Desktop.
Cross-Browser Support for HTML5 Placeholder Text in Forms
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| $.support.placeholder = false; | |
| test = document.createElement('input'); | |
| if('placeholder' in test) $.support.placeholder = true; | |
| if(!$.support.placeholder) { | |
| var active = document.activeElement; | |
| $(':text').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').blur(); | |
| $(active).focus(); | |
| $('form').submit(function () { | |
| $(this).find('.hasPlaceholder').each(function() { $(this).val(''); }); | |
| }); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment