magnobiet
2/11/2014 - 12:15 PM

jQuery Auto Focus Polyfill

jQuery Auto Focus Polyfill

$(function() {

	$('[autofocus]').autoFocusPolyfill();

});
/*!
 *  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));