Last active
June 29, 2016 04:50
-
-
Save kevinweber/ddb58d363d37a7b64713add30d53fbf1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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