Skip to content

Instantly share code, notes, and snippets.

@magnobiet
Last active August 29, 2015 13:56
Show Gist options
  • Save magnobiet/8933752 to your computer and use it in GitHub Desktop.
Save magnobiet/8933752 to your computer and use it in GitHub Desktop.
jQuery Auto Focus Polyfill
/*!
* jQuery Auto Focus Polyfill v0.1.0
* https://gist.github.com/magnobiet/8933752/edit
*
* Made by Magno Biét
* Under MIT License
*/
;(function($, window, document, undefined) {
var pluginName = 'autoFocusPolyfill',
defaults = {};
function Plugin(element, options) {
this.element = element;
this.settings = $.extend({}, defaults, options);
this._defaults = defaults;
this._name = pluginName;
this.init();
}
Plugin.prototype = {
init: function() {
if (!this.supportsInputAttribute('autofocus')) {
$(this.element).focus();
}
},
supportsInputAttribute: function(attr) {
var input = document.createElement('input');
return attr in input;
}
};
$.fn[pluginName] = function(options) {
this.each(function() {
if (!$.data(this, 'plugin_' + pluginName)) {
$.data(this, 'plugin_' + pluginName, new Plugin(this, options));
}
});
return this;
};
}(jQuery, window, document));
$(function() {
$('[autofocus]').autoFocusPolyfill();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment