Skip to content

Instantly share code, notes, and snippets.

@pospi
Created November 4, 2013 09:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pospi/7300392 to your computer and use it in GitHub Desktop.
Save pospi/7300392 to your computer and use it in GitHub Desktop.
Simple placeholder shim for IE. Functions slightly unlike browser placeholders in that the placeholder is removed immedately upon focus. To re-apply or activate elements added to the DOM post-render, simply call `.blur()` on them.
function initPlaceholderCompat()
{
var test = document.createElement('input'),
placeholderSupport = 'placeholder' in test;
if (placeholderSupport) {
return;
}
$(document).on('focus', '[placeholder]', function() {
var input = $(this);
if (input.val() == input.attr('placeholder')) {
input.val('');
input.removeClass('placeholder');
}
}).on('blur', '[placeholder]', function() {
var input = $(this);
if (input.val() == '' || input.val() == input.attr('placeholder')) {
input.addClass('placeholder');
input.val(input.attr('placeholder'));
}
}).on('submit', 'form', function() {
$(this).find('[placeholder]').each(function() {
var input = $(this);
if (input.val() == input.attr('placeholder')) {
input.val('');
}
})
});
$('[placeholder]').blur();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment