I’m trying to make a jQuery plugin for custom checkboxes and radio buttons.
(function($)
{
$.fn.checkboxRadio = function(options)
{
var defaults = some;
...
return this.each(function()
{
var button = $(this);
...
});
}
})(jQuery);
It can be used now by $('input').checkboxRadio(options);
How do I add a method check without changing current scope, to make a possible usage of something like $('input').checkboxRadio('check')?
How to handle a custom method and get its name inside my plugin?
Here is the official jquery plugin guide.
The part about wrapping functions is found here (“Plugin Methods”) (the example is a would-be tooltip plugin) :
[update] explaining the
methods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 ))line in the guide :If you call
$(selector).tooltip('update', 'hello')from your javascript code, you want to end up calling theupdatemethod, passing'hello'as thecontentargument, withthisset to$(selector)for the duration of the call.That is what this line takes care of :
method == 'update',methods[method]is theupdatemethod,argumentswill be equal to['update', 'hello'], you have to drop the first element to get the arguments you want to pass to your method ; this is exactly whatArray.prototype.slice.call(arguments, 1)does,myFunc.apply(obj, argsArray)calls the functionmyFunc, passingargsArrayas the arguments, and settingthistoobjfor the duration of the call.So inside your methods, you can call
this.each(...)to iterate over all of the selector’s items, e.g. :