Skip to content

Instantly share code, notes, and snippets.

@kevinweber
Last active June 29, 2016 04:50
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 kevinweber/ddb58d363d37a7b64713add30d53fbf1 to your computer and use it in GitHub Desktop.
Save kevinweber/ddb58d363d37a7b64713add30d53fbf1 to your computer and use it in GitHub Desktop.
/**
* Move element's class to parent element
* Usage example:
* $element.moveClassToParent('select-container');
*/
(function ($) {
"use strict";
// Create the defaults once
var pluginName = "moveClassToParent",
pluginObject = this;
// The actual plugin constructor
function Plugin(element) {
pluginObject = this;
/**
* The element the plugin was initiated on
* @type {jQuery}
*/
pluginObject.$wrapper = $(element);
}
function setOptions(options) {
pluginObject.options = options;
}
function moveSingleClass() {
var className = pluginObject.options;
if (pluginObject.$wrapper.hasClass(className)) {
pluginObject.$wrapper.parent().addClass(className);
pluginObject.$wrapper.removeClass(className);
}
}
$.fn[pluginName] = function (options) {
return this.each(function () {
if (!$.data(this, "plugin_" + pluginName)) {
$.data(this, "plugin_" + pluginName, new Plugin(this));
}
setOptions(options);
moveSingleClass();
});
};
}(jQuery));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment