Skip to content

Instantly share code, notes, and snippets.

@brianmcallister
Created February 10, 2012 19:23
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 brianmcallister/1791936 to your computer and use it in GitHub Desktop.
Save brianmcallister/1791936 to your computer and use it in GitHub Desktop.
HTML5 Placeholder polyfill
/*jslint devel: true, browser: true, indent: 2, nomen: true */
/*global jQuery, Modernizr */
// Load a polyfil for placeholder support
(function ($, Modernizr) {
'use strict';
if (!Modernizr.input.placeholder) {
var Actions = {
focusInput: function () {
var $this = $(this);
if ($this.val() === $this.data('placeholder-polyfill')) {
$this.val('');
}
},
blurInput: function () {
var $this = $(this),
text = $this.data('placeholder-polyfill');
if ($this.val() === '') {
$this.val(text);
}
}
};
$('input[placeholder], textarea[placeholder]').each(function (index, el) {
var $el = $(el),
text = $el.attr('placeholder');
$el.val(text).data('placeholder-polyfill', text);
if ($.browser.msie && parseInt($.browser.version, 10) < 9) {
$el.on('focus blur', function (event) {
var func = event.type + 'Input';
Actions[func].call(this);
});
} else {
$(window).on('input[placeholder], textarea[placeholder]', 'focus blur', function (event) {
if (event.type === 'focusin') {
event.type = 'focus';
} else if (event.type === 'focusout') {
event.type = 'blur';
}
var func = event.type + 'Input';
Actions[func].call(this);
});
}
});
}
}(jQuery, Modernizr));
@brianmcallister
Copy link
Author

Was using jQuery 1.6 on the project I wrote this for, hence delegate().

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment