Skip to content

Instantly share code, notes, and snippets.

@Bellfalasch
Created May 22, 2014 08:23
Show Gist options
  • Save Bellfalasch/c3f29483444300a83b80 to your computer and use it in GitHub Desktop.
Save Bellfalasch/c3f29483444300a83b80 to your computer and use it in GitHub Desktop.
Enables you to use the placeholder attribute on input fields in IE9 and older without any semantic changes
// 1. Use the placeholder attribute just as normal on html5 elements, most browsers will understand
// 2. The code checks if placeholder is supported, if not it will mimic the behaviour
// 3. The class ".placeholder" is added to the mimiced text, so you can style it "color: gray" in your CSS to make it all be just like in modern browsers =D
// Source: http://www.cssnewbie.com/cross-browser-support-for-html5-placeholder-text-in-forms#.U3styFiSzck
jQuery(function() {
jQuery.support.placeholder = false;
test = document.createElement('input');
if('placeholder' in test) jQuery.support.placeholder = true;
});
// Source: http://www.hagenburger.net/BLOG/HTML5-Input-Placeholder-Fix-With-jQuery.html
function fixPlaceholderAttrForIE() {
if(!$.support.placeholder) {
$('[placeholder]').focus(function() {
var input = $(this);
if (input.val() == input.attr('placeholder')) {
input.val('');
input.removeClass('placeholder');
}
}).blur(function() {
var input = $(this);
if (input.val() == '' || input.val() == input.attr('placeholder')) {
input.addClass('placeholder');
input.val(input.attr('placeholder'));
}
}).blur();
$('[placeholder]').parents('form').submit(function() {
$(this).find('[placeholder]').each(function() {
var input = $(this);
if (input.val() == input.attr('placeholder')) {
input.val('');
}
})
});
}
}
// Call the function and all is good:
fixPlaceholderAttrForIE();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment