Created
March 6, 2012 10:45
-
-
Save Schubidu/1985641 to your computer and use it in GitHub Desktop.
jquery placeholder fallback
This file contains 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
(function ($) { | |
$.support.placeholder = false; | |
var test = document.createElement('input'); | |
if ('placeholder' in test) $.support.placeholder = true; | |
$.fn.placeholderFallback = function () { | |
if (!$.support.placeholder) { | |
var active = document.activeElement; | |
this.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'); | |
} | |
}).blur(); | |
$(active).focus(); | |
$('form').submit(function () { | |
$(this).find('.hasPlaceholder').each(function () { | |
$(this).val(''); | |
}); | |
}); | |
} | |
} | |
})(jQuery); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment