Skip to content

Instantly share code, notes, and snippets.

@joladev
Last active January 4, 2016 02:59
Show Gist options
  • Save joladev/8559027 to your computer and use it in GitHub Desktop.
Save joladev/8559027 to your computer and use it in GitHub Desktop.
Faking placeholder in IE8
/**
* Set the "data-placeholder" attribute on your input fields.
* The script is only run if placeholders are not supported.
* Depends on jQuery >= 1.8
**/
(function (window, $) {
var e = document.createElement('input');
if (!('placeholder' in e)) {
$('input[data-placeholder]').each(function (idx, element) {
$(element).val($(element).attr('data-placeholder'));
});
$('body').on('focus', 'input[data-placeholder]', function () {
if ($(this).val() === $(this).attr('data-placeholder')) $(this).val('');
});
$('body').on('blur', 'input[data-placeholder]', function () {
if ($(this).val() === '') $(this).val($(this).attr('data-placeholder'));
});
}
})(window, jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment