Created
March 10, 2011 20:56
-
-
Save lunr/864912 to your computer and use it in GitHub Desktop.
jquery-place-holder-support.js
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
| // Extend jQuery.support to test for placeholder support | |
| $.support.placeholder = false; | |
| test = document.createElement('input'); | |
| if('placeholder' in test) $.support.placeholder = true; | |
| // If the placeholder attribute is not supported | |
| // ref: http://www.cssnewbie.com/cross-browser-support-for-html5-placeholder-text-in-forms/ | |
| if(!$.support.placeholder) { | |
| // save the active element for future reference | |
| var active = document.activeElement; | |
| // when an input gets focus, remove any placeholder text | |
| $('input[placeholder],textarea[placeholder]').focus(function () { | |
| if ($(this).val() == $(this).attr('placeholder')) { | |
| $(this).val('').removeClass('hasPlaceholder'); | |
| } | |
| // when an input loses focus, add placeholder text if it's blank | |
| }).blur(function () { | |
| if ($(this).val() == '' || $(this).val() == $(this).attr('placeholder')) { | |
| $(this).val($(this).attr('placeholder')).addClass('hasPlaceholder'); | |
| } | |
| }); | |
| // remove focus from all inputs, to trigger the placeholder actions | |
| $('input[placeholder],textarea[placeholder]').blur(); | |
| // restore focus to the previously active element | |
| $(active).focus(); | |
| // when the form is submitted, don't submit placeholder values | |
| $('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