When working with plugins I sometimes stumble upon one that has something like this:
- It will take constructor arguments.
- Some of the parameters resolve to functions.
- Some of those will take multiple arguements.
like this:
{
color: function (someData, moreData) {
return someData + moreData;
}
}
How do I pass two arguments to that function?
Note: I am unable to change the plugin, so moving the second argument to another constructor argument is not an option.
Sample JavaScript:
(function ($) {
$.fn.greenify = function (options) {
var settings = $.extend({
color: function (someData, moreData) { // <-- THIS TAKES 2 ARGUMENTS!
return someData + moreData;
},
}, options);
return this.css({
color: settings.color,
});
};
}(jQuery));
$("#target1").greenify({
color: "blue"
});
$("#target2").greenify({
color: ["bl", "ue"] // <-- In your answer only this should be changed
});
My targets:
<div id="target1">My Thing</div> // <-- will be blue
<div id="target2">My Thing2</div> // <-- will be black but should be blue
UPDATE: I fell for a major misconception that this code
$("#target1").greenify({
color: "blue"
});
would hand the value "blue"
over to function (someData, moreData)
as first parameter someData
. This is false! What actually happens is that the default callback (function (someData, moreData)
) is completely replaced by the string "blue"
!
$(…).select2("data", someValues, true)
). Is that what you wanted? Or do you want to know more about how to design jQuery plugins and about using.apply
? – Mi